First let me emphasize that the 301 Permanent Redirect covered here is strictly for .htaccess file redirects in an Apache Web server with the mod-rewrite module turned ON.
Therefore before you proceed, first make sure that your Website is running on an Apache Web server having the mod-rewrite module ENABLED. One way to check is to ask your hosting company.
What is 301 Permanent Redirect?
A 301 Redirect is a status code for URL redirection that tells Web browsers and search engines that a Web page has been permanently moved to a new location. In the HTTP networking protocol, a redirect is a response with a status code starting with the number 3 that cause a browser to go to a different location.
Why do you need 301 Permanent Redirect?
Because it is the most search engine friendly and efficient method for redirecting Websites. It preserves your current search engine rankings. Some of the reasons for a 301 redirect include:
- You moved your Website to a new domain.
- Users can reach your Website with different URLs. In this case it is best to pick one URL as your preferred domain, and then use 301 redirects to send the other URLs to your preferred URL. This is known as canonicalization.
- Search engines have a hard time understanding that
http://www.mydomain.comare the same. To a search engine, these two URLs are different websites with the same content, in which they may (or may not) be penalized.
- Two different Websites are merging into one page.
What is an .htaccess file?
.htaccess is an invisible text file where you can store server directives including security, redirection and how to handle certain errors. These directives apply only to the folder (and its subfolders) in which the .htaccess file resides.
To create an .htaccess file, open any text editor like Notepad or Notepad++, name it .htaccess (there is no extension).
If you already have an .htaccess file on your server, download it to your computer and open it with your favorite text editor. Once you are done with your changes, upload it to the root directory of your Website.
The code: Canonical, TLD and Root Redirection
The code below deals only with three scenarios:
- Canonical redirection: redirect www prefix to non-www
- TLD redirection: redirect net TLD to com TLD
- Root Redirection: redirect index.html and all its variations (index.php, index.cfm, default.asp, etc.) to the root (/)
I added comments (starting with #) in the code, to make it clearer for you.
If you want the preferred domain to start with www instead of the non-www, then in the code above replace “mydomain” with “www.mydomain“. It´s that simple.
Test and Test again
Last but not least test and test frequently after implementing a 301 redirect. If you find anything wrong, remove the redirect. Use a Redirect Checker tool to ensure that you are getting the correct response. Also test manually, by typing the URL in the browser´s address bar, for all scenarios and test on multiple browsers for consistency.
If after implementing the 301 redirect, the code "looks" right, but nothing seems to work, consult with your Web hosting company. There might be some permissions that needed to be set on their end.
Screen Shot Results
The two images below show the results when testing the above code using the Redirect Check Tool.
Results for http://blueoliveonline.com/
Results for http://blueoliveonline.net/. As expected the net TLD is directed to http://blueoliveonline.com/
The next two images are just curiosity from my part to see how other Websites (Yahoo and eClasses) are doing.
For Yahoo Website:
For eClasses Website:
In this tutorial, I discussed the meaning and advantages of 301 permanent redirect. I showed the code for three specific cases: canonical redirection, top level domain redirection, and root redirection. Check the references below for additional information.