5 useful url rewriting examples using .htaccess

If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess. If you don’t know something about url rewriting then please check my older post about url rewriting using .htaccess.

Now let’s look at the examples

1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

5) Redirecting the domain to a new subfolder of inside public_html.

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

126 thoughts on “5 useful url rewriting examples using .htaccess

  1. Thanks for the very clear examples…regular expressions are so confusing!

    I’m using #4 as follows:
    RewriteRule ^([a-zA-Z0-9_-]+)$ rivers.htm?r=$1 [NC]
    RewriteRule ^([a-zA-Z0-9_-]+)/$ rivers.htm?r=$1 [NC]

    Which works fine except now that when I try and link to an external URL outside the site it tries to rewrite that too. My site is http://www.WhitewaterRaftingCostaRica.com — I am hoping to make it so that if the url is in the format http://www.WhitewaterRaftingCostaRica.com/sometext then it is redirected to http://www.WhitewaterRaftingCostaRica.com/rivers.htm?r=sometext (this it does) but if the URL is any other format, then it leaves it alone.

    Can anyone help?

  2. thank you for the useful post.

  3. Nice tutorial with these 5 steps, thanks.

  4. Joanne

    I have a site set up with index.php as the main file and then all pages of the site are in a pages folder – e.g. pages/about-us.php.

    So you would browse to http://www.site.com/index.php?page=about-us and it brings up the about us page.

    I have tried using the following rewrite rule in .htaccess but with no luck:

    RewriteEngine on
    Options +FollowSymLinks
    RewriteRule (.*).html ?page=$1

    If I test it out by going to http://www.site.com/about-us.html, it comes up with Object not found error.

    Can anyone please help?

    Thanks

  5. thanks for tips to mod rewrite, but when i rewrite url (product.php?cat=1& subcat=2 change to product/1/2/bicycle.html ) the images of the page display none, how i rewrite mode of folder or file

  6. mike

    Have buillt this really big site and want to add htaccess the poblem is must I go to all hrefs and change index.php?a=2&x=3…etc.. to the “nice” URLs? This is pretty much work, I was wondering since with htaccess woy can put albums/12/1 and get albums.php?a=12&b=1 couldn’t this work the same way around meaning putting albums.php?a=12&b=1 and loading this page but also changing the address URL to the “nice” one?

  7. pat

    hey, i wonder how do you do when you have this :

    http://www.example.com/index.php?page=news&alpha=1

    and

    http://www.example.com/index.php?page=news&beta=2

    ??
    you see ?? it both calls the news page, but sometimes it is alpha, sometimes it is beta ??
    thankx a lot….

  8. Umer

    Hi,
    I am new user.
    I want to redirect
    http://myurl.com/profile/username
    to
    http://myurl.com/username

    I have tried this through .htaccess but CSS and connection breaks.

    Is there any other way to do this.

    Thanx in Advance.

  9. i think you mean for : )Rewriting product.php?id=12 to product-12.html

  10. Dear Roshan,

    Please tell me how could i rerite url for all pages for example :www.abc.com/index.php -> http://www.abc.com/home

  11. Wow, thanks. Very useful for me……

  12. Manish

    Was just wondering if you could help me with a basic url rewriting query that I have got here. My Problem is

    1) the url in the browser does not redirect to the new url (though when i enter the new url myself , it works fine)
    2) Do i need to change the links in the entire site for a particular url if i need to go for a url rewriting.

  13. Amar

    The Following Is not working,,plz help me,,,,,,,,

    Rewriting product.php?id=12 to product-12.html

    It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

    RewriteEngine on
    RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

  14. hi…

    This is very useful.

    Thank you dude

  15. lee

    worth noting, if you want to redirect all traffic to / to a new folder /new but you want to keep any existing subfolders of / working as they do, use this:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule (.*) /new/$1

    This redirects anything from “www.test.com” or test.com to /new/ but will keep http://www.test.com/otherproject/ going as it is.

  16. In first tutorial if I Rewrite product.php?id=12 to product-12.html what would be shown in browser’s address bar – product.php?id=12 or product-12.html?

  17. Hi,
    I am new user.
    I want to redirect
    http://www.mysite.com/profile/username.php?id=xxx
    to
    http://www.mysite.com/xxx

    I have tried this through .htaccess but did not solved.

    can anybody solve this problem.

    Thanx in Advance.

  18. it’s good but
    if we can trance it to php script with
    RewriteRule .* /index.php

    and in php script route the given script to actions

    thank you

  19. Nice tips.
    All work for me.
    Dont forget to load module rewrite at first.
    Because it’s been disable by default if you use xampp.

    Thanks a lot!

  20. Giridhar

    Hello.

    I want to rewrite my url . But I am having some problem in rewriting the url.

    my site url like this http://www.seomarketingireland.eu/main/frmIndex.php?Type=Home

    I need to rewrite the url http://www.seomarketingireland.eu/main/Home

    So please can u provide me the code???

    Waiting for reply..

    Byee and have a nice day.

  21. Giridhar

    Hi,
    I am new user.
    I want to redirect
    http://www.mytestsite.com/profile/username.php?user=xxx
    to
    http://www.mytestsite.com/profile/xxx

    I have tried this through .htaccess but did not solved.

    can anybody solve this problem.

    Thanx in Advance.

  22. thanks for the article, good stuff.

  23. hits

    I am using the rule for RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1 for example.com/hits-user or example.com/hits_user it shows error but works fine with example.com/hitsuser .. can you help?

  24. miguel calo

    hi roshan, is it possible to manipulate the URL if it has 2 parameters?
    for example: localhost/folder/index.php?a=1&b=2
    or you could just link me to a tutorial. thanks.

  25. raja

    my domain name http://www.xxx.com/username , i want to rewrite into http://www.username.xxx.com/ Please help this

  26. Vladimir

    I want to ask the same. How to rewrite more than one parameter? For example “index.php?a=1&b=2″ .

    Thanks

  27. Thanks for all the

  28. I thinks by using the example 3 (Redirecting non www URL to www URL) this step can help the site ranking in the search engine, because by adding this code to the htaccess the search engine will know that the two urls are referring to one site and that they are not two different sites.

  29. Hot Topic Thanks !

  30. Sujal Shakya

    Roshan Bro! Nice tut… I have one question regarding this htaccess…. How can we do redirect for sitename/username .

    I mean if i want to pass username sujal in website http://website.com ie http://website.com/sujal equivalent to http://website.com/main.php?username=sujal ..

    what should be done for this?

    Thanks in advance..

  31. Hi,

    Can u provide me the url rewriting code for my site (jspservlettutorial.info).

    Thanks.

  32. you are great man……..!!

  33. But how can i change my /public_html/folder to folder.mysite.com

    means i want to change my root folder to my subdomain with .htaccess

  34. Nice tips.
    All work for me.
    Dont forget to load module rewrite at first.
    Because it’s been disable by default if you use xampp.

    Thanks a lot!

  35. george

    Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

    Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

    It is working on localhost But when itried to fix it on web it not working.
    i put a test folder inside WWW root folder inside my test folder i placed neww .htaccess for user friendly URl ouside that there is error redirecting .htaccess i have early written when i test on testing folder file it redirect to error page .

    please tel me a solutions to solve this problem..

  36. Hi i need a little bit of help. my requirement is to fix the page name with extension and variable name and the www because i want to make my URL more short
    like below url
    OLD URL:
    http://www.globalguideline.com/index.php?JScript=first_JavaScript

    NEW URL (Required):
    http://globalguideline.com/first_JavaScript

    Please guide me,
    Regards,
    Hussain

  37. Rajat

    I used mod-rewrite in htaccess to generate clean URL with base file test.php. (I am new to php)
    Initial URL:
    http://www.example.com/test.php?id=1176
    New URL:
    http://www.example.com/test/176
    Queries are appropriately passed and printed. No problem here.

    However, the same page can get displayed by the following URL also:
    http://www.example.com/strange/1176
    I have a file called strange.php in the root. test.php and .htaccess are in the same root folder.
    Though the rule is only to change test.php to test/
    it looks like any ..php file in the root directory can substitute for test and print that URL with content from 1176, as shown above. Any help?

  38. RewriteEngine on
    RewriteRule digital-camera/([^/]+)/ brand.php?brandName=$1
    RewriteRule digital-cameras/([^/]+)/([^/]+) product_list.php?bName=$1&catName=$2

    First rule is working fine but second rule is not working, can you please tell where is the problem?

    this is my website http://www.cameraforall.com
    you see it’s working when you click on canon, nikon or any brand name but it’s not working when cliking on Compact Digital Cameras/Digital SLR Cameras

    Need help, please give me advise…

  39. Hi
    I have just found your site from google.com.I have problem .Problem is i have 12 links in page i want to click a particular link then in broswser it show domainname/linkname.But i want to when i click any link it should be show in browser with link name and each case run a file detail.php.So it is possible through
    .htacess file.Then send me code.
    Example
    AGRICULTURE SCIENCE ARCHAEOLOGY ARCHITECTURE suppose these are links when i click this link
    then result should be domain.com/link-name and each click detail.php file should be run.
    Thanks in advance
    Ranjit Kumar
    My english is not good.

  40. Raj

    Hi ,

    I am new to php and I am trying to follow you post but messed up my .htaccess file.And actually wants to redirect following URLs
    http://www.mysite.com/content/list?type=studio To http://www.mysite.com/list/type/studio

    and another URL with two query strings is :
    http://www.myxxxreward.com/content/content?type=category&prod=Anal&id=5 TO http://www.mysite.com/content/type/category/prod/Anal/id/5 and here type, prod and id values changes dynamically, like that I have 10 URL getting generated based on value from the database, how to make it work.

    Will you please help me out.

    Thank you
    Raj

  41. Hello,

    My site is in JOOMLA. I want to rewrite the home page SEF url http://67.208.116.186:81/joomla/index.php/home to the main site http://67.208.116.186:81/joomla/. Kindly guide me which code i will put in .htaccess file? I am a newbie for using apache.

    Regards,
    Murtaza

  42. thanks for the nice examples

  43. Really useful examples, thanks for sharing them

  44. Very useful!!!

    Tks 😉

  45. Mark

    I have tried the technique for number 4

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?page_id=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?page_id=$1

    The URL that the link in picking up is
    http://79.170.44.125/platinumbrides.co.uk/page.php?page_id=events

    I want to get it to be

    http://79.170.44.125/platinumbrides.co.uk/events

    I am getting a page not found error

    What am I doing wrong?

  46. i need to underscore for my url, how i can do it pls help me out

  47. Hi,

    My page is http://www.sndnads.com/tv.html

    i want is like http://www.sndnads.com/tv

    Please help me out…

  48. Florencia

    This is the one example that worked for me. Thanks, very clear and usefull. Saved me lots of time.
    Regards

  49. Very useful rewriting rules especially the no.4 been searched for.

  50. puneet

    Hi,

    I want to know how I can change
    http://www.mywebsite.com/index.php
    to
    http://www.mywebsite.com

    Thanks,
    puneet

Comments are closed.