.htaccess 301 Redirects
I quite often see questions asking about .htaccess redirects and 301 redirects etc in a number of forums and blogs I am a member at, so I thought it might be nice to post list of different .htaccess redirects which are commonly used in making a site more seo friendly.
This thread is also very helpful for those in need of changing from an old site to a new site.
Before we begin editing any .htaccess code, make sure to download a backup copy of the current one just in case something goes wrong as the .htaccess file is very temperamental and can mess up your site if you enter the incorrect code.
Option 1. www to non-www 301 redirect:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
This will make sure all url’s without www. redirect to the www. version of your site. For example nathanielbailey.co.uk would redirect to www.nathanielbailey.co.uk
Option 2. non-www to www 301 redirect:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This redirects all www. url’s to the non-www. version. For example www.nathanielbailey.co.uk would redirect to nathanielbailey.co.uk
You should use either option 1. or option 2. to make sure search engines only see the single copy of your website as well as help to make sure all link juice and domain weight is pushed to a single url instead of spreading it out across the two.
index.php to domain.com/ redirect (change extension as needed):
Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
Use this to redirect your index.php default.php home.html or what ever you use to your root domain. For example www.nathanielbailey.co.uk/index.php redirects to www.nathanielbailey.co.uk
This should be use to help stop duplicate pages on search engines as well as make sure that only the single copy of your page gets all the weight from links etc.
301 redirect old page (change extension as needed):
Redirect 301 /old-page.html http://www.domain.com/page.php
If you have any old pages for what ever reason you should make sure to redirect them to the new page (if applicable) or at least redirect them to your home page or a relevant page, so not to have broken links and not to lose out on any old links and link juice.
Redirect Entire Folder/Directory
RewriteRule ^old/(.*)$ http://www.domain.com/new/$1 [R=301,L]
The above line of code will redirect the entire folder to a new folder no matter what the url! The same can be done to redirect an entire folder to your root directory by simply removing /new from the line of code. For example if you wanted to “redirect entire folder to root domain” you would use:
RewriteRule ^old/(.*)$ http://www.domain.com/$1 [R=301,L]
Another option would be to use the “RedirectMatch rule like so:
RedirectMatch 301 ^/old/$ http://domain.com/
This single line will also redirect all requests for the old directory “/old” to your site’s root directory, meaning domain.com/old/anything-here will redirect to domain.com/anything-here as it matches the url minus the old directory.
Change file extension (change extension as needed):
RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
This is a simple fix for those that have made the change from .html to .php I updated one of my other sites, all of which used domain.com/page.html which I changed to domain.com/page.php so I could do more with the site.
This can also be used for any extension you like, simple change the .html and/or .php to the extension’s’ you require.
Subdomain Redirects
First of all if you want to your redirects to work correctly you will need to create a new .htaccess file in the subdomain folder you are working from as placing these redirect code’s’ in your root folders .htaccess file will likely not work at all on most server set-ups.
Redirecting Subdomain to non-www
Redirect 301 /ts3/ http://old.domain.com
RewriteCond %{HTTP_HOST} ^new\.domain\.com$
This will make sure your subdomain url is redirecting to the correct place, IE: sub.domain.com rather then www.sub.domain.com or sub.domain.com/sub.
This is an important redirect to have in place, so I always use it all on my subdomains just to make sure they all redirect to the correct url and follow the same url structure site wide.
Remember, this code is placed in your .htaccess in the subdomain, not your root folder!
Redirect subdomain to subdomain
Remember if you want to redirect sub.domain.com to new.domain.com you will need to first create a new .htaccess file in the original subdomain folder for this to work correctly.
RewriteEngine on
Redirect permanent / http://new.domain.com/
Don’t forget this code should have been placed in the .htaccess file in old.domain.com not your root folder or it wont work!
A couple of bits to remember:
Options +FollowSymLinks
RewriteEngine on
- 1. The above should only be used once in any .htaccess file and will always be placed before your redirect code as above examples
- 2. .htaccess will not work on windows, its only for Linux.
- 3. upload .htaccess files in ASCII mode, sending it up as binary will break it.
- 4. Be sure to check for a current .htaccess file, as “.” files are hidden so check your settings to make sure you can see them.
- 5. Remember to change domain.com to your domain!
- 6. Last of all, always check your changes to make sure its working correctly.
There are other ways of doing 301 redirects so if you cant use .htaccess have a search on google and I’m sure you will find what you need, if not simple ask me and I would be more then happy to write up a simple post like this one for other types of redirects you require information on.
**EDIT**
Ok so I have been working on a standard .htaccess file which I can simple slap on all my new sites and client sites without having to make any changes or remember whats doing what! And here it is for all to enjoy
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Rewrite .php to extensionless URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#
# Externally redirect requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.domain.com/$1 [R=301,L]
#Above line has to use your url not HTTP_HOST as that dont work!
(Note: The opinions expressed on this blog in its posts and replies are the opinions of their respective authors and not those of www.nathanielbailey.co.uk)
The much awaited BF3 map pack for Back To Karkand is finally here!…




I work in Online Marketing Development for some medium to large size companies in the UK doing SEO, Online Marketing and Coding. I like to spend my spare time playing online games such as BC2 Vietnam with my friends, and playing around with websites such as building them, making changes and keeping content up to date etc. I also work on and run a number of sites for online solutions from web hosting to game servers and much more. Read more about me and my sites at


