Latest TweetsWordPress and the Blank Target Vulnerability (aka rel noopener + noreferrer): perishablepress.com/wordpress-… #WordPress #security #html
Perishable Press

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.

  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 monzillamedia.com, and I also have a domain named jjjstarr.com
  • A request is made for something.jjjstarr.com, which doesn’t exist
  • The server returns the content from my primary domain, monzillamedia.com, with the address bar still showing something.jjjstarr.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.jjjstarr.com, they’ll get the following:

  • The primary domain’s home page (instead of the actual, requested domain)
  • The URL remains as images.jjjstarr.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.jjjstarr.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.jjjstarr.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 (jjjstarr.com in our example)
  • The URL of the requested domain (i.e., (http://jjjstarr.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.

Jeff Starr
About the Author Jeff Starr = Web Developer. Book Author. Secretly Important.
Archives
12 responses
  1. 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.

  2. 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?

  3. 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.

  4. 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!

  5. Videos Futbol January 12, 2011 @ 6:24 am

    Thank

    this is a great information, I prefer with http://www...

  6. Thanks so much for doing this.

  7. I think I made a mistake. Do I have to do this if the site on Media Temple is a WordPress one?

  8. Jeff Starr

    @Yael: Yep, most of my sites are WordPress ones ;)

  9. 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?

  10. 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 to http://site.com?ID=(wildcard).

    nothing I have found so far can accomplish this. Any luck from your end?

  11. 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

  12. 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…

[ Comments are closed for this post ]