How to Setup Canonical Domain Names with Apache | WebReference

How to Setup Canonical Domain Names with Apache

By Sukrit Dhandhania


Canonicalization is the method used to setup your web server so that it picks the best URL when there are several choices. This usually refers to the home page of a website. For example, most people would consider the URLs sevenacross.com, www.sevenacross.com, www.sevenacross.com/index.html all the same URL. However, as far as your web server is concerned all of these are unique and different URLs. Using canonical URLs you can merge all of these so that your web server treats them all the same in a web browser. This has a positive impact on search engine optimization. Not using canonical URLs has a negative impact on your SEO, as Google notices that several URLs are returning the same content and penalizes you.

What is a Canonical Domain Name?

Canonical domain name is the full form of CNAME. You might have encountered this term while setting up your domain name. CNAME is an entry, which is a part of your domain's DNS records. A computer hosting a website has an IP address, which is used to access the content over the web. DNS helps us avoid distributing numeric IP addresses to people and handles the conversion from domain names to IP addresses. This is pretty straightforward when you have one website being served by an IP address. However, you might want to serve various websites with the same IP address and the same domain name. This can be done using subdomains such as forums.sevenacross. The subdomains are handled by the domain's CNAME records.

Many hosting services atomically add a CNAME entry for the subdomain www so that people accessing your website as sevenacross.com or www.sevenacross.com are both directed to your site. This might seem like a good idea and quite helpful, but it's not really a good thing. Google sees two copies of everything and you lose points on the SEO front. Let's see how to fix this issue.

Fixing the Problem

This problem has a rather simple fix. If you are familiar with URL rewriting in Apache then you should be able to skip straight to the code below. For the rest I'll give a brief explanation of how to implement the fix. If you are on a dedicated server and have access to your server's Apache configuration file, which is usually located at /etc/http/conf/httpd.conf, then you can add the code below to the file. If you are on a shared environment, you might not have access to edit the httpd.conf file. In such a case, you can create a file called .htaccess with the content below and place it in the root folder of your website.

Remember to edit the code shown above and replace the domain sevenacross.com with your own domain. Save the file. You will need to restart your web server if you chose the method where you edited the httpd.conf file. The code above takes all requests to your website that come with the access beginning with http://sevenacross.com and redirects them to http://www.sevenacross.com. This works for all sub-URLs as well. So if a user types the address http://sevenacross.com/photos/albums.php she will be automatically directed to http://www.sevenacross.com/photos/albums.php. All this while keeping Google happy.

Fine Tuning

There might be a scenario where you want to use your website without the www at the beginning of the domain. You would need to modify the code above slightly to make that happen:

You can apply a similar method to redirect your users to a home page. Say you want all users access the address http://www.sevenacross.com/index.php to be redirected to http://www.sevenacross.com You can redirect users to this address automatically by adding the following configuration to your Apache configuration file:

You can replace index.php with anything else. You might want to change the extension to .html or .asp, or to home.php. Make sure you run a check of the settings after you apply them. A pretty simple fix for a potentially big SEO headache.