Note: Windows Servers use different instructions, which can be found in this posting
Our blog posting on verifying 301 redirects led to several questions about the technical details of redirects. Therefore, we are posting some information which you may find useful.
Below you will find out how to do canonical 301 redirects, redirect individual pages, and redirect whole domains.
301 redirects for Linux/Apache servers are created using a special file called .htaccess. Once the redirects have been specified in this file, it is simply uploaded to the root of the hosting account and the server handles it from there. 301 redirects can be easily created in this way for both shared and dedicated hosting accounts.
Very special note: This information is by no means comprehensive. Creating .htaccess files should be done carefully. If you already have an HTACESS file, you should back it up, so if anything goes wrong, you can revert to the old code!
Creating a 301 Redirect to Solve Canonicalization Duplicate Content
Canonicalization duplicate content is created when a site shows content at http://www.domain.com and http://domain.com. Search engines consider this 2 separate sets of content (even though they’re both exactly the same), and this can sometimes negatively impact the site’s rankings.
A 301 redirect can be set up to force all non-www URLs to the corresponding www URLs, or vice versa. In addition to eliminating any potential duplicate content issues, this will also combine the strength of all links pointing to both non-www and www pages on the site, allowing the site to maximize the impact of any link to its pages.
REQUIREMENT: The following 301 redirect instructions will NOT work unless the Apache ModRewrite module is enabled on the hosting server. This is a standard Apache module that is normally enabled by default, so if the redirects don’t work within 24 hours of uploading the .htaccess file you will need to contact the hosting company or administrator to check if ModRewrite is enabled.
From non-www to www:
- FTP into the site’s hosting account and check to see if there’s an existing .htaccess file.
- If there’s not, create a file in Notepad or any text editor called .htaccess (technically, this file is just an extension with no name).
Note: Some programs may not let you create a file with no name and just an extension, and if you need to email this file to a webmaster for implementation many email clients such as Outlook may refuse to send it. To get around this, create the file with a regular name like “test.htaccess”, and then rename it in an FTP client right before uploading - Paste the code below this list into the site’s .htaccess file:
- Upload the file into the root directory of the site’s hosting account.
Once the .htaccess file has been uploaded, it should take effect within 24 hours or less. If it does not, check with the hosting company or administrator to make sure that the ModRewrite module is enabled for the site’s hosting account.
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
From www to non-www:
- FTP into the site’s hosting account and check to see if there’s an existing .htaccess file.
- If there’s not, create a file in Notepad or any text editor called .htaccess (technically, this file is just an extension with no name).
Note: Some programs may not let you create a file with no name and just an extension, and if you need to email this file to a webmaster for implementation many email clients such as Outlook may refuse to send it. To get around this, create the file with a regular name like “test.htaccess”, and then rename it in an FTP client right before uploading - Paste the code below this list into the site’s .htaccess file:
- Upload the file into the root directory of the site’s hosting account.
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yoursite.com
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Once the .htaccess file has been uploaded, it should take effect within 24 hours or less. If it does not, check with the hosting company or administrator to make sure that the ModRewrite module is enabled for the site’s hosting account.
Creating a 301 Redirect from One Internal Page to Another
- FTP into the site’s hosting account and check to see if there’s an existing .htaccess file.
- If there’s not, create a file in Notepad or any text editor called .htaccess (technically, this file is just an extension with no name).
Note: Some programs may not let you create a file with no name and just an extension, and if you need to email this file to a webmaster for implementation many email clients such as Outlook may refuse to send it. To get around this, create the file with a regular name like “test.htaccess”, and then rename it in an FTP client right before uploading - Paste the following code into the site’s .htaccess file:
Redirect 301 /oldpagename http://domain.com/newpagename
Note: If you need to redirect multiple pages, simply add a new line to the .htaccess file for each page you’d like to redirect. - Upload the file into the root directory of the site’s hosting account.
Once the .htaccess file has been uploaded, it should take effect within 24 hours or less. If it does not, check with the hosting company or administrator to make sure that the ModRewrite module is enabled for the site’s hosting account.
Creating a 301 Redirect from an Old Domain to a New One
- FTP into the site’s hosting account and check to see if there’s an existing .htaccess file.
- If there’s not, create a file in Notepad or any text editor called .htaccess (technically, this file is just an extension with no name).
Note: Some programs may not let you create a file with no name and just an extension, and if you need to email this file to a webmaster for implementation many email clients such as Outlook may refuse to send it. To get around this, create the file with a regular name like “test.htaccess”, and then rename it in an FTP client right before uploading - Paste the following code into the site’s .htaccess file:
Redirect 301 / http://www.newdomain.com/ - Upload the file into the root directory of the site’s hosting account.
Once the .htaccess file has been uploaded, it should take effect within 24 hours or less. If it does not, check with the hosting company or administrator to make sure that the ModRewrite module is enabled for the site’s hosting account.
For special cases and other server configurations, there are multiple sites online which are server specific and give more instructions about how to do 301 redirects. As we stated above, 301 redirects should be done carefully, and if the site fails to operate after an HTACESS file is uploaded or change, you should revert back to the old code until a fix can be made.




Here is something useful.
This will run down the 3 criteria first, redirecting to a specific location. If none of the first three are applicable, the user just gets redirected to the homepage of the new site
Redirect 301 /page2.html http://www.newdomain.com/page2.html
Redirect 301 /any_dir http://www.newdomain.com/any_dir
Redirect 301 /pic.jpg http://www.newdomain.com/pic.jpg
RedirectMatch (.*) http://www.newdomain.com
Sadly, redirection to a new url by using the .htaccess file is sometimes prevented by the host not enabling that capability, as you suggest in the post. In that regrettable situation, the host company will sometimes offer their own redirection service, but it will not deliver the SEO benefits of a proper 301 redirect. Fight hard, and hopefully they'll concede and enable the capability. But ultimately, the skills needed for this situation will probably be far greater than those needed for even the most complex coding. In the meantime, let's hope that such hosting companies will become less protective and more supportive.
Hi Lorne and thanks for the comment. Yes it is frustrating when a hosting company has this limitation.
If you are looking at doing 301 redirects within the same domain, you can implement the "canonical link element" which Matt Cutts has referred to as a mini 301 redirect. Again, that would only work if wanting to redirect a url to another url within the same domain. Good luck!