Home > Search Engine Optimization (SEO), Web Development > How to Use Mod_Rewrite to Set a Canonical URL

How to Use Mod_Rewrite to Set a Canonical URL

October 31st, 2008

The importance of the Canonical URL is well known in the SEO world. However, for most web developers and website owners is something that is often overlooked. The theory in short explains that search engines rank each page individually and typically penalize multiple pages with duplicate content. So, if a site does not have a mechanism that identifies a Canonical URL, (in other words, a unique URL for the main page), search engines may evaluate multiple links that result in the home page separately. As a result, your site may get penalized altogether or simply suffer from lower pagerank due to the fact that the pagerank is now shared among multiple pages. An example:

The home page of a site can be displayed with any one of the following URLs:

http://domainname.com

http://domainname.com/index.html

http://www.domainname.com

http://www.domainname.com/

etc.

To avoid the above, simply use apache’s mod_rewrite and include the following code in an .htaccess file that should be located at the root folder of the web server (replace the domain name text with your real one):
RewriteCond %{HTTP_HOST} !^www.domainname.com$ [NC]
RewriteRule ^(.*)$ http://www.
domainname.com/$1 [R=permanent,L]

Now, you’ll need to make sure that all your links that directs traffic to the home page use the chosen canonical url.

Search Engine Optimization (SEO), Web Development , , , , , ,

Comments are closed.