301 Permanent Redirect for an Apache Web Server

301 permanent redirectFirst 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:

  1. You moved your Website to a new domain.
  2. 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.
  3. Search engines have a hard time understanding that http://mydomain.com and http://www.mydomain.com are 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.
  4. 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:

  1. Canonical redirection: redirect www prefix to non-www
  2. TLD redirection: redirect net TLD to com TLD
  3. 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.

########  code 301 redirect start  ########
#Rewrite www to non-www 
#Rewrite index.html (also index.php, index.cfm, etc.) to / 
#Rewrite domain.net (also www.domain.net) to domain.com

#RewriteEngine directive enables the runtime rewriting engine.
RewriteEngine On

#FollowSymLinks is a directive in your Web server configuration  
#that tells your Web server to follow symbolic links. It is an 
#important setting for your Website security.
Options +FollowSymLinks

#declare a RewriteBase to give it something to start from.
RewriteBase /

#Rewrite www to non-www (preferred domain is non-www); 
#! means NOT ; [NC] means Not Case sensitive.
#The ! also redirects all other domains parked in the same webspace.
#Like the .net if you own it.
#An example of 'mydomain.com' is 'blueoliveonline.com' 
RewriteCond %{HTTP_HOST} !^mydomain\.com [NC]
RewriteRule (.*) http://mydomain.com/$1 [R=301,L] 

#Redirect index.html to / 
#i.e. redirect http://mydomain.com/index.html to http://mydomain.com/
RewriteCond %{THE_REQUEST} ^.*/index\.html 
RewriteRule ^(.*)index.html$ http://mydomain.com/$1 [R=301,L]

#Redirect index.php to / 
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://mydomain.com/$1 [R=301,L] 

#Redirect index.htm to / 
RewriteCond %{THE_REQUEST} ^.*/index\.htm
RewriteRule ^(.*)index.htm$ http://mydomain.com/$1 [R=301,L] 

#Redirect index.cfm to / 
RewriteCond %{THE_REQUEST} ^.*/index\.cfm
RewriteRule ^(.*)index.cfm$ http://mydomain.com/$1 [R=301,L] 

#Redirect index.html to / 
RewriteCond %{THE_REQUEST} ^.*/index\.asp
RewriteRule ^(.*)index.asp$ http://mydomain.com/$1 [R=301,L] 

#Redirect index.aspx to / 
RewriteCond %{THE_REQUEST} ^.*/index\.aspx
RewriteRule ^(.*)index.aspx$ http://mydomain.com/$1 [R=301,L]

#Redirect index.shtml to / 
RewriteCond %{THE_REQUEST} ^.*/index\.shtml
RewriteRule ^(.*)index.shtml$ http://mydomain.com/$1 [R=301,L] 

#Redirect index.pl to / 
RewriteCond %{THE_REQUEST} ^.*/index\.pl
RewriteRule ^(.*)index.pl$ http://mydomain.com/$1 [R=301,L]  

#Redirect default.htm to / 
RewriteCond %{THE_REQUEST} ^.*/default\.htm
RewriteRule ^(.*)default.htm$ http://mydomain.com/$1 [R=301,L]  

#Redirect default.asp to / 
RewriteCond %{THE_REQUEST} ^.*/default\.asp
RewriteRule ^(.*)default.asp$ http://mydomain.com/$1 [R=301,L]
########  code 301 redirect end  ########   

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.

Also set your preferred domain in Google Webmaster Tools

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/

Redirect Check Tool for Blue Olive Online com domain

Results for http://blueoliveonline.net/. As expected the net TLD is directed to http://blueoliveonline.com/

Redirect Check Tool for Blue Olive Online net domain

The next two images are just curiosity from my part to see how other Websites (Yahoo and eClasses) are doing.

For Yahoo Website:

Redirect Check Tool for yahoo.com

For eClasses Website:

Redirect Check Tool for eClasses.org

Conclusion

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.

References

If you reached that far and liked this post, please link back to it. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

3 Responses to “301 Permanent Redirect for an Apache Web Server”

  1. Hello Boutros
    I am endeavouring to redirect /index.html to the Home Page with the following script in the .htaccess file on an Apache server with Mod Rewrite enabled to the directory where this website is published.
    RewriteCond %{HTTP_HOST} ^.*/index\.html
    RewriteRule ^(.*)index.html$ http://www.4starcottages.co.uk/$1 [R=301,L]
    Unfortunately it is not working and I shall be grateful for your comments, as I have found your article so helpful. Many thanks and regards, Richard.

    • Hi Richard,
      I am not sure if I understood what you want.
      You want to redirect “http://www.4starcottages.co.uk/index.html” to “http://www.4starcottages.co.uk/”
      If this is the case, then add the following code to your .htaccess file:

      #RewriteEngine directive enables the runtime rewriting engine.
      RewriteEngine On
      Options +FollowSymLinks
      #declare a RewriteBase to give it something to start from.
      RewriteBase /
        
      #Redirect ht tp://www.4starcottages.co.uk/index.html to ht tp://www.4starcottages.co.uk/
      RewriteCond %{THE_REQUEST} ^.*/index\.html
      RewriteRule ^(.*)index.html$ ht tp://www.4starcottages.co.uk/$1 [R=301,L]
      

      Hope this helps.
      (Note: I wrote ‘ht tp’ instead of ‘http’, so that WordPress does not create a link. Make sure to correct it in your code.)
      Boutros.

  2. Jeremy S. says:

    Excellent design and info. you got here! Thank you for sharing your ideas and putting the time into this. Great work!