301 Redirects: Apache, htaccess, and SEO | Cardinal Path Blog
Blog

301 Redirects: Apache, htaccess, and SEO

 

This was originally posted in 2008, but not a lot has changed. Given how many times we’ve seen improper redirects during our SEO Mistakes series, we’ve decided that this deserves a repost. Enjoy.

Apache server provides a great feature to help webmasters with SEO. This feature is htaccess file. This file is located in the root of websites and provides additional setting information to server. Webmasters have access to this file and can easy adjust it for specific needs.

Let’s take a look how htaccess can help optimize website hosted on Apache server.

1. There are many discussions about non-www and www versions of websites. What version is better for SEO? Neither is better. Just choose one version and stick with it. It will help search engines do not index the same pages under different URLs (with and without www).

Example below redirects from domain.com to www.domain.com

 

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

 

Please note that Google Webmaster Central has a tool to select preferred version of domain (with or without www).

2. Many websites have internal links pointed to homepage of website as http://www.domain.com/index.html . At the same time homepage is accessible under domain as http://www.domain.com . As result, search engines could index homepage (the most important page on website) under 2 different URLs. One line of htaccess code helps avoid or fix it.

 

RewriteRule ^index.html http://www.domain.com/$1 [R=301,L]

 

3. What to do if you want to move a web page in different directory or rename file/directory? Think about that twice. Do you really need it? It’s better to keep URLs of all pages stable and don’t change it. In case you have to move/rename page or directory make sure you setup permanent (301) redirects from old URLs to new ones. Code below shows an example of this redirect.

 

Redirect 301 /old_file.html http://www.domain.com/new_file.html

 

Code below shows an example of htaccess file that includes all lines we reviewed above

 

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule ^index.html http://www.domain.com/$1 [R=301,L]
Redirect 301 /old_file.html http://www.domain.com/new_file.html
Redirect 301 /old_dir/file.html http://www.domain.com/new_dir/file.html

 

Make sure you test website after any update of htaccess file. Wrong code or any mistake can put down entire website.

From David Eckman, left in the comments:

One other type of 301 that wasn’t discussed is when you want to do a redirect from one URL to another within the same hosting account. This ensures that everyone must use a single domain to access your site.

The code for doing this is below:

 

What this basically says, is that if the domain used to access the site is NOT www.domain-to-use.com, then do a 301 redirect to that domain.

 

Copyright © 2016, All Rights Reserved. Privacy and Copyright Policies.