Canonical URLs and Subdomains with Plesk
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.
- 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”.
- 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). - 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’shttpdocs
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
monzillamedia.com
, and I also have a domain namedexample.com
- A request is made for
something.example.com
, which doesn’t exist - The server returns the content from my primary domain,
monzillamedia.com
, with the address bar still showingsomething.example.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.monzillamedia.com
, 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.example.com
, they’ll get the following:
- The primary domain’s home page (instead of the actual, requested domain)
- The URL remains as
images.example.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 (404 link removed 2016/11/28), which redirects all requests for nonexistent subdomains to the actual, requested parent domain. So in our example, a request for images.example.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.example.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 (
example.com
in our example) - The URL of the requested domain displayed in the address bar (i.e.,
https://example.com/
)
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.
12 responses to “Canonical URLs and Subdomains with Plesk”
So great, and thank you for sharing. I’ve been thinking about moving to DV on MediaTemple for sometime now, and so your sharing your experience is great. I hadn’t even thought that I’d need to do this, and you’ve explained how to once I do jump in. Thanks again.
funny you post this today Ann over at trafficgenerationcafe.com had a post talking about a very similar topic. (also an earlier post I read here on this was was Jan 08…).
Though I know you hate things not being ‘built in’ but does not an SEO plugin for WP work whether it is in plesk or not?
2 for 2 on awesome articles. We always encourage customers, with write ups like these, to add to our wiki articles. If you ever have the time, we’d love to see this added to our library. Thanks again for the support, and feel free to let us know if you need any help.
This is a well timed post. I just created a new website and Google Webmaster Tools mentioned that I wasn’t utilizing URL canonicalization. I tried accessing my site with and without WWW (I prefer with) and was always redirected properly. I also noticed WordPress had already wrote what I thought was the necessary redirect in the htaccess file, but I added your “enforce www prefix” script to the file. I’ll see in a couple days whether or not Google is satisfied.
After a quick browse through your site and reading this helpful article, I’m certainly adding it to my bookmarks. Thanks Jeff!
Thank
this is a great information, I prefer with www prefix
Thanks so much for doing this.
I think I made a mistake. Do I have to do this if the site on Media Temple is a WordPress one?
@Yael: Yep, most of my sites are WordPress ones ;)
I thought I read somewhere that for WordPress who don’t have to do canonical URLs in the htaccess. That the URL in Settings-General controls canonicalization. Am I incorrect?
Jeff – thanks for the great write up. I was similarly in the process of doing the items above but ran into another issue I’m hoping you’ve found a solution for. Ultimately, it appears Media temple has something built into their apache configuration which takes every subdomain literally and regardless of what is listed within the .htaccess file, it will seek out an actual directoy. I am trying to direct customers to
http://(wildcard).site.com
and have the URl automatically redirected tohttp://site.com?ID=(wildcard)
.nothing I have found so far can accomplish this. Any luck from your end?
Hello
—————
I need to convert this URL
for exampel:{like wordpress}
...../?p=26 ==> ...../user.php?username=26
I use this .htaccess but not work
Options +FollowSymlinks
RewriteEngine On
RewriteRule ?p=([a-zA-Z0-9_-]+)$ user.php?username=$1
please help me little …
excuse me my language is persian
if my speaks not true
——————–
THANKS
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).
Where is the Domain Administrator button. Can find it anywhere…