Pretty URLs always helps you to boost up search engine page ranking. In this post I want to explain about URLs rewriting techniques using .htaccess file. Friendly URLs looks neat on the browser address bar. Take a look at live demo

Download Script
Live DemoLong days back I had discussed about .htaccess basic rewriting tips Eg. First parameter Domain.com/users?id=9lessons to Domain.com/9lessons take a look the following link.
This time I'm talking about rewriting second parameter URLs using .htaccess file.

Original URL.
Eg. http://flickr.com/users.php?id=username&page=2
Rewriting Friendly URL.
Eg. http://flickr.com/username/2
.htaccess Code
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Twitter like Pretty URLs

.htaccess Code
Just rewriting first parameter.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
index.php
Contains simple PHP code.
<?php
$key=$_GET['key'];
if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
$key=$_GET['key'];
if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>









Simple and useful. Thanks!
share knowledge that is useful, thank you
Great post :-)
A great intro to using mod_rewrite... One thing I do a little differently is to use the "?" character like so:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ users.php?user=$1
That way the regex will match both "something" and "something/"
Very nice!
switch($_GET['key'])
{
case 'home': include('home.php');break;
case 'login': include('login.php');break;
case 'terms': include('terms.php');break;
case 'users':include('users.php');breaak;
default: include('404.php');
}
Much better.
Agree with the previous poster. (I was going to say the same thing)
Thanks for the info
Its really helpful.
but can u please explain this code as I have no idea about .htaccess file and this script.
Hi
i want a script for preventing XSS attacks in PHP.
can u please help me...
it will be so nice of u..
thnx
hi Srinivas, great works man. i need a little help, i'm trying to rewrite a url with two variables.. view.php?id=1&title='some text';
the problem is, the 'id' queries my database but in the final result i only want the 'title' variable displayed along the url without the 'id' showing. how do i write the .htaccess code. Thanks
@keno
Can u send mail what exactly you want with url examples to srinivas[at]inbox.com
how can I add third parameter?
I've got a problem, when I use 2 or 3 parameters, I've got worries with the relative links of my css, my js.. because they refers to the numbers of / in the url and they think they are in a subfolder.. Any ideas?
why not like this:
if(isset($_GET['page'])){
$key = $_GET['page'];
} else{
$key = "home";
}
require($page .".php");
use .htaccess nice but css file problem
hi , i want to rewrite my url www.mybazaar.pk/details/property/index.php?id=77
TO
www.mybazaar.pk/details/property/77/
what shold i write in my htaccess file ? and what more i have to change my in my site ?
this is giving the 500 internal server error on my localhost and i am using wamp server.
Can anyone help me how can i remove this error.
Thanks for the information,
and i want to make sure I understand with correct method or not, and I write some example here.
if in user.php got more than 1 variable is it like this to write in .htaccess?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?friends=$2
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?likes=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?friends=$2&page=4
sorry if I'm wrongly understand and my bad english.
Thanks
maybe i know already
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
mean
^([a-zA-Z0-9_-]+) user=$1 will change only result include a-z A-Z 0-9
and
([0-9]+)$ page=$2 will change only result include 0-9
if user=abcd&page=2
mean will become
domain.com/abcd/2
is it?
it works thanx but if you add at the add an extra slash -> / to your site url http://flickr.com/username/2/ <- slash aaded, you can see the root that document..
is there some fix for that?
It should work
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Nice article
i have an url like below
http://www.bodyftn.com/article.php?cate=2&page=2
I want it to display like this
http://www.bodyftn.com/tips/page2
how can i do this? kndly help me..
Does it work in localhost or not ?
this site is very use full thank u boss
i like this site and functionality
Hey srinivas,
how we redirect like facebook username
www.facebook.com/profile.php?username=srinivas&v=info
to
www.facebook.com/srinivas?v=info
?? facebook username
www.facebook.com/profile.php?username=srinivas&v=info
to
www.facebook.com/srinivas?v=info
??
does it work in localhost? i'm using xampp and i'd already configured apache. thanks in advance.
Hi, What is the best way to allow
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
for more than one php file? at the moment i have
users.php?user=
and stations.php?station=
how can i make htacess differ between the 2?
Thanks,
Jamie.
RewriteEngine On
RewriteRule ^mydir/([^/]*)\.php$ /mydir/index.php?id=$1 [L]
I Got
"Internal Server Error"
Please I really need your help on htaccess. Below is my htaccess and it's not working
RewriteEngine on
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)/$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ user?user=$1
My url does work if I redirect to say
localhost/user?user=eniga
To
Localhost/user/eniga
Please how can I get this to work
Enigaahiante@aol.com. Tnx
when i type ç,ö and other turkish words, i get server not found response, how can i solve this ?
thank you for this tutorial
hey, u missed
RewriteBase
Hi i can't use your code tell me why?
Hi i need to rewrite url which has two parameters i could not figure out how to rewrite i tried many forums but i didnt. can i ve solution.
still its not working check here is the link where i have used it
http://espeeglobal.com/facilities.php
click on any image it will open an album than you will see the parameter query not the rewrite.
My code for htaccess is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.espeeglobal.com
RewriteRule (.*) http://espeeglobal.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ facilities-album.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ facilities-album.php?id=$1
Hi its not loading css file when using
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
For those of you having problems with your css or js files loading after implementing url rewrites, be sure you're either using absolute paths (ie, /path/to/file.css as opposed to path/to/file.css), have added the rel attribute set to canonical (ie, link rel="canonical" href="yourdomain.com"), or added a base filepath (base href="yourdomain.com"). These fixes should be applied to the html page that includes the css or js.
The problems you're having stem from the server looking for the files within a directory that doesn't actually exist, or in one that doesn't contain the desired file.
cat it be done on localhost
i am using wamp on windows, i hv turn on mod rewrite but its not working on my local host please help me what should i do....
this is url
http://localhost/OFFICE_PROJECTS/project1/categories.php?cid=52
i want to rewrite as
http://localhost/OFFICE_PROJECTS/project1/categories/52
Hi Srinivas,
Thanks for simple code for such complecated process. your example code is working but I want to use it bit differently. I'm passsing ID and then username of that id in database should be in URL like if I pass this URL
http://www.mysite.com?id=4
Then the URL should be this:
http://www.mysite.com/username
(username of id=4)
Please tell me how can I do that?
Its really a wonderful blog. I learned a lot about htaccess RewriteRule from here. Thank u all guys n gals.
Hi. I use this script
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
but when it loads, css doesn't included. and i also trapped in that folder. how do i solve this? thanks