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.
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.
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).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!
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:
monzilla.biz, and I also have a domain named cssresetr.comsomething.cssresetr.com, which doesn’t existmonzilla.biz, with the address bar still showing something.cssresetr.comWhether 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:
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:
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:
cssresetr.com in our example)http://cssresetr.com/) displayed in the address barAnd 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.
Source: Perishable Press
Take your WordPress skills to the next level with Digging into WordPress!
Read more: http://perishablepress.com/press/2011/01/06/canonical-urls-subdomains-plesk/