Ultimate .htaccess rewrite tutorial with 301 redirects

So, over the last couple of weeks I have moved several sites to new locations and publishing platforms which demands some redirects unless you wanna be a SEO killer. The examples below are mostly URLs with query strings which I either want to hide or make prettier. The fourth and fifth examples are quite useful when you want to create human readable URLs for APIs or web services.

Updated 22 November 2011.

1. Rewrite and redirect URLs with query parameters (files placed in root directory)

Original URL:


Desired destination URL:


.htaccess syntax:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]

2. Redirect URLs with query parameters (files placed in subdirectory)

Original URL:


Desired destination URL:


.htaccess syntax:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]

3. Redirect one clean URL to a new clean URL

Original URL:


Desired destination URL:


.htaccess syntax:

RewriteEngine On
RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]

4. Rewrite and redirect URLs with query parameter to directory based structure, retaining query string in URL root level

Original URL:


Desired destination URL:


.htaccess syntax:

RewriteEngine On
RewriteRule ^([^/]+)/?$ index.php?id=$1 [QSA]

5. Rewrite URLs with query parameter to directory based structure, retaining query string parameter in URL subdirectory

Original URL:


Desired destination URL:


.htaccess syntax:

RewriteEngine On
RewriteRule ^/?category/([^/]+)/?$ index.php?category=$1 [L,QSA]

6. Domain change – redirect all incoming request from old to new domain (retain path)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]

If you do not want to pass the path in the request to the new domain, change the last row to:

RewriteRule ^(.*)$ http://www.example-new.com/ [R=301,L]


  1. Pingback: Linux by sansogm - Pearltrees
  2. THANK YOU so so so much for this write up!
    I redesigned my site using WordPress and removed my vbulletin forum.
    All my vbulletin redirects were hitting 404 and this helped me out immensely in getting proper 301 redirects!
    Thank you again,

  3. Pingback: Štruktúra URL adries | BE VISIBLE! blog o internetovej reklame a marketingu
  4. Thanks a lot for this post.
    If i want to redirect not only from index.php. Sometimes from index.php, other times on from catalogue.php and more.
    How to do this.

    Kind regards

  5. My URL is http://www.codingprogrammer.com/html/html-show.php?page=HTML%20Attributes
    but i want to change
    how to change this from .htaccess and

    http://www.codingprogrammer.com/html/index.php to

    please Send revert my query ???

  6. Thanks a lot!

  7. Can you please help me with this:
    Actual URL: http://www.example.com/webpage.php?id=100&name=keyword
    Desired URL: http://www.example.com/webpage/keyword/
    Thank you!

  8. I have zero knowledge in htaccess and I got this htaccess code to redirect:
    RewriteEngine On
    RewriteRule ^teamfront.php?/?$ http://tresbizz.com/ [L,R=301]
    It is redirecting fine from:
    but not linking to resp. page,linking to home page instead, anybody please help me. Thanks in advance

  9. Can you please help me with this:


    Thank you!

  10. I’d like only to change file name e redirect old urls to news (same parameters).
    page.php?id1=1&id2=4 -> Redirect 301 -> page2.php?id1=1&id2=4

    Thank you

  11. Dude, seriously?

    RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]

    Will fuck any URL with a d in it. Pull the d out.

  12. Hi. I need to re-direct url, means need pretty url. For example, index.php?id=2 to http://www.websitename.com/history.
    I’ve added .htaccess code but its not working locally or on online server. Kindly guide me.
    RewriteEngine On
    RewriteRule ^/?history/([^/d]+)/?$ index.php?id=$2 [L,QSA]

    On the other hands i need to re-direct http://www.websitename.com/index.php?id=1 to http://www.websitename.com/home

  13. Awesome Code.. Thnks guys.
    but i have one doubt, can you please solve it?

    Original URL:

    Desired destination URL:

    is it possible?

  14. Hi Finn,

    Thanks for sharing your knowledge!
    I am a Ph.D. Student, running some tests on Internet, and I need something that could allow me to create a link that keeps the parameters of the current page url.

    Current url:

    Url link in that page:

    That would help me, you’ve no idea!

    Thanks again,

  15. Am moving from a Windows ASP setup to a Linux PHP setup. Would it be possible to framecode the redirect so that all asp files are redirected to a php file of the same name. In case of query strings the same query strings should be passed on.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>