Using 301 redirect in htaccess file

Last updated on April 30, 2010. Tags: , ,

Supposed you decided one day to change the domain name of your website for whatever reasons. The catch is that your old domain name already attained a high page rank and that lots of other websites already link to your old domain. What would you do then?

Perhaps, you are considering the following solutions:

The first two options has several disadvantage. In the first solution, you assume that the home page is the only page through which visitors can enter your site. However, it is possible for other webmasters to link to your pages other than the home page, and search engines can also index these other pages. People who are suppose to visit your website through these pages will be instead redirected to a '404 not found' message.

The second solution is labor intensive. You need to tweak the code of each individual pages to enable the redirect using meta refresh. Moreover, it is not practical (but still possible) if you are using a content management system (CMS) such as WordPress wherein pages are generated by a set of PHP codes instead of separate HTML websites. Moreover, search engines tend to penalize this kind of redirect because it is frequently used in various black hat SEO techniques.

Hence, we are left with the third option, using the .htaccess file to setup 301 redirect.

Setting-up 301 redirect between old and new domain

To setup 301 redirect from an old domain (www.olddomain.com) to a new domain (www.newdomain.com), open the file manager of your old domain and you should find a file .htaccess. If you are using CPanel, be sure that you don't check the option that makes system files invisible for you to see the .htaccess. Open the .htaccess in edit mode, paste the following code and save it.

redirect 301 / http://www.newdomain.com/

Note that there is a single character space before and after the first slash. Simply replace the www.newdomain.com with whatever new domain name you would like to move your website to.

If you don't have .htaccess file, you can create it if you are using CPanel. I am not sure about the other server management system. You cannot create it in Windows even if you expose the extension name because Windows will insist that you create a filename (it counts htaccess as extension name because it is preceded by a period).

Unfortunately for Yahoo! small business users, there is no way to access the .htaccess file and if you create it, the 301 redirect will not function. This is one big reason why you should not avail Yahoo! small business.

How does it work?

The above code will work perfectly assuming that you use exactly the same file names and directory structure in the new domain as in the old domain. For example, you have page1.htm in the root directory, page2.htm and page3.htm in folder1, and page4.htm in folder2. When you move to your new domain, these filenames and subdirectory (or folder) names should remain.

If you did it correctly, when a web surfer typed www.olddomain.com/page1.htm in the address bar of his or her browser and press enter, he or she will be automatically redirected to www.newdomain.com/page1.htm. If another website links to your page www.olddomain.com/folder1/page2.htm, clicking on that link will automatically redirect the visitor to www.newdomain.com/folder1/page2.htm.

The same will happen for /folder1/page3.htm and /folder2/page4.htm, and for any pages for that matter as long as the filenames and directory structure (and subdirectory names) are preserved. You will actually see the address in the address bar changing from www.olddomain.com/something to www.newdomain.com/something whenever redirection happens.

Search engines prefer 301 redirect because it is less prone to abuse. When a search engine robot crawls your website through a link that is directed to www.olddomain.com/page1.htm, the robot will automatically go to www.newdomain.com/page1.htm.

The search engine robots see the 301 redirect as a signal that they should now delete from their index pages and files from the old domain and index those that are in the new domain. Of course, it will take some time before search engines can purge all old URLs and replace them with new ones. The good thing is that, when these old pages appears in search results, visitors are automatically directed to the new pages.

Redirection of embedded files

301 redirect also works for individual files. If you are calling a source image file img src="http://www.olddomain.com/images/file.jpg", the subdirectory naming "images" that contain "file.jpg" is already in the new domain, and a 301 redirect was setup, the image will still appear on the page where it is being called.

However it is unlikely that you will need to use 301 redirect to call external (non HTML) files and embed it to HTML files. It is either you will change all the calling of external files from the old domain to the new domain or you are using relative URL for your external files.

What if I changed a file name, a folder name or folder hierarchy?

Suppose you are moving files from www.domain.com to www.domain2.com but some of the file names and directory structure was changed. Let's say you want to redirect from the files and folders in the old domain to new domain as follows:

www.domain.com/page1.htm                    --> www.domain2.com/page1.htm
www.domain.com/page2.htm                    --> www.domain2.com/page2.htm
www.domain.com/home.htm                     --> www.domain2.com/main.htm
www.domain.com/folder1/page11.htm           --> www.domain2.com/page4.htm
www.domain.com/folder1/image1.jpg           --> www.domain2.com/image1.htm
www.domain.com/folder1/image2.jpg           --> www.domain2.com/image2.htm
www.domain.com/folder2/folder21/page211.htm --> www.domain2.com/page5.htm

Encoding a simple redirect 301 / http://www.domain2.com/ in the .htaccess in the root directory of www.domain.com will not work. For example, you will be redirected to file home.htm and folder /folder1/, which does not exist in www.domain2.com.

To properly redirect the pages and files, we need to have several lines of 301 redirect with the inner or more specific change in filename or directory structure coming first.

redirect 301 /folder2/folder21/page211.htm http://www.domain2.com/page5.htm
redirect 301 /folder1/page11.htm http://www.domain2.com/page4.htm
redirect 301 /folder1/ http://www.domain2.com/
redirect 301 /home.htm http://www.domain2.com/main.htm
redirect 301 / http://www.domain2.com/

Notice the space before http://? It is the only thing that separates the new domain from the old domain.

You need to start with the most specific or innermost file or folder because the server will only bypass them and go to the next if they do not apply. For example. the topmost line will apply only if the page being visited is www.domain.com/folder2/folder21/page211.htm. The third line will apply to any files in www.domain.com/folder1/, redirecting to the same file (or subdirectory) in the root directory of www.domain.com. The last line applies to everything else.

If you start with the more general, let's say putting redirect 301 / http://www.domain2.com/ on top, this is what will happen. If the user visited, say www.domain.com/folder1/page11.htm, the user will be redirected to www.domain2.com/folder1/page11.htm because the server recognizes that the more general redirect still applies, and just ignore the more specific redirects below it. If such URL does not exist, then the user will get a 404 not found.

Conclusion

The 301 redirect through .htaccess file is more useful in most cases as compare to other forms of redirect. You don't need to edit each HTML file one by one and search engines recommend them unlike what they do to other forms of redirect. Even though there are cases wherein meta refresh and javascript redirect can me more useful, there is no doubt that learning 301 redirect is essential in web development.

Posted by Greten on April 9, 2009 under Search Engine Optimization (SEO), Server Configuration

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.