Setting Canonical Domain with Apache

An experiment in search engine optimization:
My work site, (or if you’re intimate) is also known by three other domain names, because of past re-branding. My problem? How to tell search engines that these are the exact same site, so they know that an external link to, say, is to count as a link to (and boost my site’s Google ranking, goddamit!). Establishing a canonical domain name like this should also help consistency of brand (i.e. helping the user know what site they are on and what to call it).

For a long time I had a <base href=”…”> tag in my home page to set the canonical domain. This is dumb. It only ensures that a user sees the domain once they’ve come to the home page and then clicked a link. A check shows that, and still exist in the Google index as separate sites. A serious fix requires a few lines of Apache config:

RewriteEngine on
RewriteCond %{HTTP_HOST} (ltsn|heacademy)
RewriteRule (.*)$1 [R=301,L]

In English, “If the request is pointed at a domain containing the strings ‘ltsn’ or ‘heacademy’, capture the complete path (the rest of the URL after the domain) and send the user a permanent redirect to the canonical domain plus that path.”

Something that took a while to get, and which caused a headache when I was setting the config for another site, was that if you have these directives in a .htaccess file rather than your main config file, the path is not assumed to have the initial slash, so you’d have to have a slightly different version.

RewriteEngine on
RewriteCond %{HTTP_HOST} (ltsn|heacademy)
RewriteRule (.*)$1 [R=301,L]

Google Webmaster Tools allow you to set whether or not you want the www. prefix to appear in the canonical domain of your site as it appears in search results. Will this apache tweak have any significant effect on search engine placement? I’ll report the results.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: