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:
http://www.example.com/index.php?id=1
Desired destination URL:
http://www.example.com/path-to-new-location/
.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:
http://www.example.com/sub-dir/index.php?id=1
Desired destination URL:
http://www.example.com/path-to-new-location/
.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:
http://www.example.com/old-page/
Desired destination URL:
http://www.example.com/new-page/
.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:
http://www.example.com/index.php?id=100
Desired destination URL:
http://www.example.com/100/
.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:
http://www.example.com/index.php?category=fish
Desired destination URL:
http://www.example.com/category/fish/
.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]
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,
Jay
Thanks for the comment Jay, glad I could help you. Good luck with your forum!
Finn
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
My URL is http://www.codingprogrammer.com/html/html-show.php?page=HTML%20Attributes
but i want to change
http://www.codingprogrammer.com/html/HTML%20Attributes
how to change this from .htaccess and
http://www.codingprogrammer.com/html/index.php to
http://www.codingprogrammer.com/html/home
please Send revert my query ???
# For url http://www.codingprogrammer.com/html/html-show.php?page=HTML%20Attributes you could try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ html-show.php?page=$1
# Not tested but it should work for your site
Thanks a lot!
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!
Rajiv
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:
http://tresbizz.com/teamfront.php?Adobe-Acrobat-XI-Standard
to
http://tresbizz.com/?Adobe-Acrobat-XI-Standard
but not linking to resp. page,linking to home page instead, anybody please help me. Thanks in advance
Can you please help me with this:
from
http://www.ejemplo.com/wordpress/?p=995
to
http://www.ejemplo.com/blog/
Thank you!
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
Dude, seriously?
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
Will fuck any URL with a d in it. Pull the d out.
Thanks for spotting that. Fixed!
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
Awesome Code.. Thnks guys.
but i have one doubt, can you please solve it?
Original URL:
http://www.example.com/index.php?category=fish&name=test
Desired destination URL:
http://www.example.com/fish/name/test/
is it possible?
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:
http://abc.de/fgh?a=1&b=2&c=3
Url link in that page:
http://anotherwebsite.com/bla?a=1&b=2&c=3
That would help me, you’ve no idea!
Thanks again,
Dim.
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.