I’ve been asked this question time and time again, so here we are. How to redirect directories and old URL’s to new ones when moving your site from one domain to another, or launching a new version with re-structured URLs.
Here’s a sample structure of a re-direct line:
Redirect 301 /about.php
/about.php here is the local URL relative to the core domain you’re redirecting from. If you’re redirecting from then this will be /directory/about.php and if you are redirecting from then it is simply /about.php.
The second part of this statement is the complete URL you are forwarding to.
The beauty of a 301 redirect with .htaccess is that if you have one big set of URL’s that have changed from one place to another with the filename/end URL staying the same it is still the same statement. For example:
Redirect 301 /test/
This redirects /test/about.php to – anything after /test/ will be redirected to and placed at the end of the URL. The same applies for all query URLs (i.e. about.php?variable=1&this=that).
Tip: Always priority order your redirects. The redirects you want done first should go up top. But you should also be careful not to place inherent statements above specific ones.
This won’t work:
Redirect 301 /test/
Redirect 301 /test/about.php
This will result in the first statement being processed and the second statement meaning nothing. The correct order is:
Redirect 301 /test/about.php
Redirect 301 /test/
This means the specific statement is applied first, and then the inherent statement is ran afterwards.
Here’s a sample of extensive re-directs for Twod.co.uk to JHuskisson.com:
Redirect 301 /general-posts/ http://jhuskisson.com/general-news/
Redirect 301 /wp-content/uploads/11/ http://jhuskisson.com/wp-content/uploads/
Here is what they do:
- Moves all posts in the /general-posts/ slugged category to show at /general-news/ on the new site. For example /general-posts/testing-post is now /general-news/testing-post on the new site as we changed the category.
- All uploads from /wp-content/uploads/11/ are now linked to in /wp-content/uploads/. For examples /wp-content/uploads/11/test.jpg is now /wp-content/uploads/test.jpg on the new server.
All of these lines can be applied to 302 (temporary) redirects too. Just switch the number 301 with 302 to achieve the result you need. 302 redirects should be used where the file is only moving there for a short while but will be put back eventually and 301 redirects are used to transfer the URL to the new URL permanently and work great for a site move or a URL restructuring.
I used these to transfer Twod.co.uk to JHuskisson.com and several other transfers and they have all retained by page rank and search engine strength from domain to domain with ease.
Have any other htaccess redirect tips or things to discuss? Feel free to leave a comment using the form below.
Edwin —
Thnx! Just what I needed right now, perfect timing :)
Tina —
Thanks! This is exactly what I was looking for.
Carlos Hermoso —
Very useful post. Thanks