5 useful url rewriting examples using .htaccess

Posted on March 11, 2008 
Filed Under htaccess, php, tips and technique

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

Popularity: 75% [?]

If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» How to check and enable mod_rewrite module in apache
» Hide .php extension with url rewriting using .htaccess
» How to redirect browser to https (ssl) in php
» Custom Error Page with .htaccess

Comments

31 Responses to “5 useful url rewriting examples using .htaccess”

  1. John Tittle on March 12th, 2008 3:55 am

    I’m thinking about optimizing my url query strings, but want to make sure there isn’t a basic problem with the structure of my site first.

    I recently changed my website from www-dot-dofsbo-dot-com to searches.tittlerealestate.com. The content remains very similar. Suddenly its as if the content of my site is no longer getting crawled. Hits from keywords are limmited to phrases on my home page. The tittlerealestate domain has been around for a long time and should be getting crawled.
    Could the “searches” portion of my url be causing the problem? What do you call the portion of a url preceeding the main url?

    Thanks for the article

  2. Roshan on March 13th, 2008 11:22 am

    well you should have done 301 redirect to the new transferred domain then deleted the content from the old site. I think there is problem with the same content in both place i.e the massive duplicate content problem

  3. Max on March 25th, 2008 9:54 pm

    #2 - The title says the opposite of what its actually doing.

    #3 - I’m nitpicking but
    ^optimaxwebsolutions\.com
    will also match optimaxwebsolutions.com.otherwebsite.com

    #5 - Maybe I’m wrong here but I believe rewrite conditions (RewriteCond) must all be true for the RewriteRule to take place. So these two lines
    RewriteCond %{HTTP_HOST} ^test\.com$
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    can’t both be true, so this rule will never do anything. I think you want to use the [OR] flag.

  4. Luke L on March 25th, 2008 10:31 pm

    In the second example you put the value in pid, not id, that might catch newbies out.

    Also, in the fourth example you don’t check for existing directories, so if someone were to go to yoursite.com/about they would get the userpage for About.

  5. Roshan on March 26th, 2008 4:55 am

    Luke, thanks for the heads up..i’ve corrected that one…and in the fourth example you should have rewrite condition if you’ve the about about up page in that manner..

    Max Thanks for the comment, I think i’m valid at point #2. And the other points are you made are absolutely right and i’ve corrected them. Thank for noticing that……

  6. 5 useful url rewriting examples using .htaccess on March 26th, 2008 5:04 am

    [...] Go! never in my life Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

  7. Jason on March 26th, 2008 5:31 am

    Hm.. having meaningful and clean urls could be done originally with something like Django. Where instead of the page being

    product.php?id=12

    You could actually just have..

    /products/12

    or /whatever/ you want by modifying the URLS file in the app’s directory.

    A much cleaner and more elegant solution.

  8. Roshan on March 26th, 2008 6:12 am

    hi jason, I agree with you elegant solution but it doesn’t show the keyword in the URL and from seo point of view it’s better to put the keyword in the URL of the post.

  9. How to re-write URLs using .htaccess | The How To Do Things Blog on March 26th, 2008 7:58 am

    [...] provide this feature, then you can tweak your .htaccess file to re-write URLs. Roshan on his blog tells you how to re-write your URLs using your .htaccess file. Technorati Tags: re-write URLs, [...]

  10. cherouvim on March 26th, 2008 8:43 am

    Does [2] mean that the following urls will all point to the same resource?

    product/ipod-nano/12.html
    product/foobar/12.html
    product/test/12.html
    product/example/12.html

  11. Roshan on March 26th, 2008 9:00 am

    ya cheruvim..it all point to the same page because according to the rewrite rule it will all call to the same page product.php?id=12

  12. Boyan Kostadinov on March 26th, 2008 12:40 pm

    Decent examples, thanks.

    However, I am a bit confused with the example vs. the caption. Such as:
    1) Rewriting product.php?id=12 to product-12.html
    but your rule is rewriting in the opposite way such as product-12.html to product.php?id=12

    Same thing applied for #2 and #4.

    The syntax for the rewrite rules is:
    RewriteRule Pattern Substitution (see http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule)

    I am missing something?

  13. Fünf nützliche Beispiele für URL Umleitungen unter Benutzung der .htaccess Datei on March 26th, 2008 8:39 pm

    [...] Original:5 useful url rewriting examples using .htaccess [...]

  14. Les Geek Links de Jean Rat … (26/03/08) | T'as le bonjour de Jean Rat ! on March 26th, 2008 10:04 pm

    [...] 5 useful url rewriting examples using .htaccess - 5 useful url rewriting examples using .htaccess [...]

  15. Roshan on March 27th, 2008 10:35 am

    dear Boyan Kostadinov, well don’t be confused … as you know the normal way of displaying url is product.php?id=12 and since we’re rewriting product.php?id=12 to something else the caption is like so..hope this helps..

  16. March’s Best Links | Vandelay Website Design on March 29th, 2008 2:35 pm

    [...] 5 Useful URL Rewriting Examples Using .htaccess - from Roshan’s Blog. [...]

  17. rascunho » Blog Archive » links for 2008-03-29 on March 29th, 2008 8:41 pm

    [...] 5 useful url rewriting examples using .htaccess 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. (tags: roshanbh.com.np 2008 mes2 dia29 at_home url .htaccess blogging url_rewriting) [...]

  18. Le blog de Vincent Battaglia » En vrak - 74 on April 5th, 2008 3:44 pm

    [...] Quelques exemples simples d’URL rewriting… [...]

  19. How to check and enable mod_rewrite module in apache of xampp or wamp on April 7th, 2008 5:42 am

    [...] and enable mod_rewrite module in apache after reading two most of the popular post of this blog 5 useful url rewriting examples and hide .php extension with url rewriting. How to check weather mod_rewrite module is enabled or [...]

  20. lord55 on May 7th, 2008 6:35 pm

    hi!!!
    i have enable rewrite.my htaccess start with:

    # ugly ugly hack to detect missing mod_rewrite
    # RedirectMatch must be to an absolute destination, so forces 500 error…
    ErrorDocument 500 “Elgg error: Apache does not have mod_rewrite loaded. Please check your apache setup.”
    RedirectMatch 302 .* index.php

    order allow,deny
    deny from all

    # Don’t listing directory
    Options -Indexes

    # Follow symbolic links
    Options +FollowSymLinks

    # Default handler
    DirectoryIndex index.php

    # php 4, apache 1.x

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    # php 4, apache 2

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    # php 5, apache 1 and 2

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    RewriteEngine on

    i call a javascript from a header.php in the root inside “folder1″. i.e:

    -root
    htacess
    main.php
    header.php
    –folder1

    the script is:

    but the javascript doesn’t work and in firebug i have an error:
    syntax error
    [Break on this error] <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.or…

    …and i see in firebug an index page.

    can you give me an help to set this rule nad teach me why?!?! i try and try but no result!
    thanks in advance!!!

  21. lord55 on May 7th, 2008 6:39 pm

    the javascript i call with:
    script type=’text/javascript’ src=’folder1/?project=time’></script

    from header.php

  22. tevan on May 14th, 2008 2:46 pm

    thanks for this, roshan! I just used it at the last minute before launching a new site today.

  23. Roshan on May 14th, 2008 2:56 pm

    nice to hear that tevan…

  24. danny on May 21st, 2008 8:00 pm

    Dude!!! I spent 3 hours total yesterday messing w/ mod_rewrite and tutorials from other sites and I could figure out #4. Now that I know, life is tons easier!!!

    p.s. thats why I donated, bc of your great help

  25. Roshan on May 22nd, 2008 5:45 am

    wow danny nice to hear that…..you’re the first person to donate in this blog ….

  26. Kenai on May 27th, 2008 11:41 pm

    Man… I found this site today via google, and I’m bookmarkin’ it! I’m a very, very new PHP/mySQL dev, so I will be using this site tons! When I get some funds from commercial work, I will be donating. Thank you very much!

  27. Roshan on May 28th, 2008 3:02 pm

    nice to hear that Kenai..

  28. Sunil on June 16th, 2008 6:38 am

    Hello.

    I want to rewrite my url which has two parameter. But I am having some problem in rewriting the url.

    I need to rewrite the url product.php?id=12&action=detail to product-12-detail.html.

    So please can u provide me the code???

    Waiting for reply..

    Byee and have a nice day.

  29. Dru on June 26th, 2008 2:15 am

    test - this does not seem to work for me.

  30. Dru on June 26th, 2008 2:51 am

    The question is: if a user goes to: http://www.someisp.com/ they actually are getting served from http://www.someisp.com/store/ yet they only see http://www.someisp.com/

  31. DotNetGuts on July 24th, 2008 8:53 pm

    Check out the simplest way of writing URL Rewriting

    http://dotnetguts.blogspot.com/2008/07/url-rewriting-with-urlrewriternet.html

Leave a Reply