Monday, September 12, 2016

How to Setup Canonical Domain Names with Apache


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.comwww.sevenacross.comwww.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.
1Options +FollowSymlinks 
2RewriteEngine on 
3RewriteCond %{HTTP_HOST} ^sevenacross\.com 
4RewriteRule ^(.*)$ http://www.sevenacross.com/$1 [R=301,L]  
view plain | print | ?
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.conffile. The code above takes all requests to your website that come with the access beginning withhttp://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:
1Options +FollowSymlinks 
2RewriteEngine on 
3RewriteCond %{HTTP_HOST} ^sevenacross\.com 
4RewriteRule ^(.*)$ http://sevenacross.com/$1 [R=301,L]  
view plain | print | ?
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:
1Options +FollowSymlinks 
2RewriteEngine on 
3RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
4RewriteRule ^(.*)$ http://www.sevenacross.com/$1 [R=301,L]  
view plain | print | ?
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.

No comments: