Canonical URLs and Subdomains with Plesk

.htaccess made easy

I am in the process of migrating my sites from A Small Orange to Media Temple. Part of that process involves canonicalizing domain URLs to help maximize SEO strategy. At ASO, URL canonicalization required just a few htaccess directives:

# enforce no www prefix
<IfModule mod_rewrite.c>
 RewriteCond %{HTTP_HOST} !^domain\.tld$ [NC]
 RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>

When placed in the web-accessible root directory’s htaccess file, that snippet will ensure that all requests for your site are not prefixed with www. There’s also a force-www technique if that’s how you roll. Either way, the point is that on most shared hosting, URL canonicalization is simple.

Canonical URLs using Plesk

Using Plesk at Media Temple on a dedicated virtual (dv) machine, URL canonicalization requires a few more steps. I am no Plesk/(mt) expert, but this is what works for me. As with everything, your mileage may vary.

  1. Choose your primary domain – in Plesk, click on Server > IP Addresses. In the far-right “Hosting” column of your IP list, click on the number. You should see your domains listed. Pick your primary and click “Set As Default”.
  2. Enable www – This is the trick: even if you don’t want to use the www prefix, you need to enable it before canonicalizing your URLs. So, for each of your domains, click on the “Domain Administrator” button and put a checkmark in the “www” box (near the top of the options list).
  3. Canonicalize via htaccess – once your primary is set and all domains have the www feature enabled, just add the following htaccess code to each domain’s httpdocs directory:
# enforce no www prefix
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
</IfModule>

Be sure to replace example.com with your domain name. Alternately, you may want to enforce www:

# enforce www prefix
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
 RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>

That’s it! Your domains should now resolve to either the www or non-www URL. Remember to test thoroughly!

Adding & canonicalizing other subdomains

Technically, the www prefix is treated as a subdomain. On (mt) (dv), all non-existent subdomain URLs are by default routed to the primary domain. Here is an example:

  • My primary domain is monzilla.biz, and I also have a domain named cssresetr.com
  • A request is made for something.cssresetr.com, which doesn’t exist
  • The server returns the content from my primary domain, monzilla.biz, with the address bar still showing something.cssresetr.com

Whether or not this default functionality is problematic depends on your situation, but it can cause problems when actual, existing subdomains are involved. For example, I have a subdomain, images.monzilla.biz, that will eventually host my image content. Creating the images subdomain in Plesk is easy, but if someone requests the images subdomain via another domain, such as images.cssresetr.com, they’ll get the following:

  • The primary domain’s home page (instead of the actual, requested domain)
  • The URL remains as images.cssresetr.com in the address bar (even though that’s not what they are looking at)

Not really the best user experience, but that’s the default functionality as of the writing of this post. Fortunately, there are at least two ways of handling this situation.

One solution is to configure a wildcard subdomain, which redirects all requests for nonexistent subdomains to the actual, requested parent domain. So in our example, a request for images.cssresetr.com (which doesn’t exist) would result in the following:

  • The requested domain’s home page displayed for the nonexistent URL (better)
  • The URL remains as images.cssresetr.com in the address bar (even though that’s not what they are looking at)

This is an improvement, but we’ve still got to deal with that incorrect URL showing in the address bar. The way that I did this (and I’m sure there is an easier way) involves adding a little htaccess magic to the primary domain’s httpdocs directory:

# plesk subdomain fix
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%2.%3/$1 [L,R=301]
</IfModule>

Blam! That’s the magic right there. Once in place, this code will ensure that all requests for non-existent subdomains return the following:

  • The home page of the requested domain (cssresetr.com in our example)
  • The URL of the requested domain (i.e., (http://cssresetr.com/) displayed in the address bar

And best of all is that real, existing subdomains resolve as normal/expected. For my setup, this is the ideal functionality for setting up righteous canonical URLs and subdomains on Plesk/(mt). Remember to test like the dickens, and please let me know if there is a better way to do this! Thanks.