Wall Script
Follow Me:
Monday, November 23, 2009

Pretty URLs with .htaccess Rewriting Tips.

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

Pretty URLs with .htaccess Rewriting Tips.

Download Script     Live Demo

Long 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

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

Contains simple PHP code.


include('home.php'); // Home page
else if($key=='login')
include('login.php'); // Login page
else if($key=='terms')
include('terms.php'); // Terms page
include('users.php'); // Users Gateway
Jônatan Froes said...

Simple and useful. Thanks!

Anonymous said...

share knowledge that is useful, thank you

Chris Williams said...

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/"

Vishnu said...

Very nice!

Anonymous said...

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.

Anonymous said...

Agree with the previous poster. (I was going to say the same thing)

Rajesh Kanuri said...

Thanks for the info

Arvind.b said...

Its really helpful.
but can u please explain this code as I have no idea about .htaccess file and this script.

Arvind.b said...

i want a script for preventing XSS attacks in PHP.
can u please help me...
it will be so nice of u..

keno said...

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

Srinivas Tamada said...


Can u send mail what exactly you want with url examples to srinivas[at]inbox.com

Anonymous said...

how can I add third parameter?

Anonymous said...

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?

Anonymous said...

why not like this:

$key = $_GET['page'];
} else{
$key = "home";

require($page .".php");

Anonymous said...

use .htaccess nice but css file problem

MANNEE said...

hi , i want to rewrite my url www.mybazaar.pk/details/property/index.php?id=77

what shold i write in my htaccess file ? and what more i have to change my in my site ?

only said...

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.

kenny said...

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.


Kenny said...

maybe i know already
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2


^([a-zA-Z0-9_-]+) user=$1 will change only result include a-z A-Z 0-9


([0-9]+)$ page=$2 will change only result include 0-9

if user=abcd&page=2
mean will become

is it?

Anonymous said...

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?

Srinivas Tamada said...

It should work

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2

perumal said...

Nice article
i have an url like below

I want it to display like this

how can i do this? kndly help me..

Anonymous said...

Does it work in localhost or not ?

nikunj said...

this site is very use full thank u boss

nikunj said...

i like this site and functionality

Supun Praneeth said...

Hey srinivas,
how we redirect like facebook username




?? facebook username





Anonymous said...

does it work in localhost? i'm using xampp and i'd already configured apache. thanks in advance.

Jamie Warren said...

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


and stations.php?station=

how can i make htacess differ between the 2?


Chirag Shah said...

RewriteEngine On
RewriteRule ^mydir/([^/]*)\.php$ /mydir/index.php?id=$1 [L]

I Got

"Internal Server Error"

Anonymous said...

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



Please how can I get this to work

Enigaahiante@aol.com. Tnx

Anonymous said...

when i type ç,ö and other turkish words, i get server not found response, how can i solve this ?

thank you for this tutorial

softwarefreak said...

hey, u missed

sovannhelps said...

Hi i can't use your code tell me why?

hari said...

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.

~~~~~Vivek~~~~~ said...

still its not working check here is the link where i have used it
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

Rambo said...

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

Anonymous said...

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.

vinod said...

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


i want to rewrite as


Dhaval said...

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


Then the URL should be this:

(username of id=4)

Please tell me how can I do that?

Purna Rao said...

Its really a wonderful blog. I learned a lot about htaccess RewriteRule from here. Thank u all guys n gals.

ijhal said...

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

Anonymous said...


I need help How can i redirect multiple pages

Ex: 1.mysite.com/xxx.php to dfd
2.mysite.com/ddd.php?id=testst to mysite.com/testst
2.mysite.com/dffff.php?id=dfdfd to mysite.com/dfdfd

Vishal said...

Hi Srinivas,

Good job man. I read your posts almost daily.

I have a doubt here,

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
Suppose the rewrite rule is written as above, the url will be rendered as

Now, will I be able to use $_GET global variable to fetch the 'name' and '2'?
If yes, could you please let me know how?

Anonymous said...

My htaccess files isn't working on my cpanel. What is the problem?

mohammad yasir said...

plz help me :(
i am using this code in .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /TM/testing.php?user=$1

Anonymous said...

better use switch case than if else. switch case more faster than if else.

Monu said...

Thank you so much Srinivas. This was exactly what I was looking for. Used it and works like a charm!!

NIlay said...

Nice URL Re-writing ...

pankaj thakur said...

vishal you are right i have also doubt on multi parameter url.

Anonymous said...


I need help How can i redirect multiple pages

Ex: 1.mysite.com/ex1.php to ex1
2.mysite.com/ex2.php?id=A3xd to mysite.com/ex2/A3xd
2.mysite.com/ex3.php?id=100 to mysite.com/ex3/100

thank you

Anonymous said...

I want to do like facebook and write http://mydomain/name or email with @ and dot symbols accepted instead of http://mydomain.com/jobpro.php?act=xxx

I am using following code :

RewriteEngine on
Options -Indexes
RewriteEngine On
RewriteRule ^([_a-zA-Z0-9-&+$#*=@]+)$ etweet.php?act=$1

to rewrite http://mydomain.com/jobpro.php?act=xxx
when someone types http://www.mydomain.com/xxx
Ex: displayed as http://www.mydomain.com/yyy@gmail.com
or http://www.mydomain.com/rajeev.jobs
or http://www.mydomain.com/rajeev
or http://www.mydomain.com/rajeev123456
so typed url http://www.mydomain.com/xxx
is fetched internally http://mydomain.com/jobpro.php?act=xxx
and displayed as http://mydomain.com/xxx

The problem is with dot and http://mydomain.com/yyy@gmail.com and http://www.mydomain.com/rajeev.jobs is giving error 404 Object not found

Code works fine without dot but when I include dot . or \. to symbols like below :

RewriteRule ^([_a-zA-Z0-9-&+$#*=@\.]+)$ etweet.php?act=$1
RewriteRule ^([_a-zA-Z0-9-&+$#*=@.]+)$ etweet.php?act=$1

http://mydomain.com/yyy@gmail.com or http://mydomain.com/rajeev.jobs works but http:mydomain.com url itself gets internally redirected to
http://mydomain.com/jobpro.php?act=xxx which is not desired

It seems code reads .com in main url and redirects main site domain url

Requirement is almpost like facebook


where xxx can be name or email containing any symbols @ and .

App development services said...

Excellent post

join us said...

Its really a wonderful blog.

Kavya Rane said...

Hi its not loading css file when using

RewriteEngine on

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ product.php?user=$1&page=$2

8TETMyAt said...

i want to Write Pretty URL.... RewriteCond method is need??

Anonymous said...

hi i want my Url
to be writen like

For my .htaccess i have used the following code
RewriteRule ^sarees$ :8080/public_html/sub-products?catid=$1 [L]

How can i do this?
is there any code i need to include in my php file too apart from .htacceess?

Lokesh theCrazzy said...

how didi you made subdomains in your website like
i can't get it?

Tom said...

Awesome !!! It work like charm ....
I use it to make pretty URL affiliate web script for my client

Anonymous said...


I want my url

to be written like this


Post a Comment