Mugo Web main content.

Subdomain rewrite rule strategy: whitelisting

By: Peter Keung | January 3, 2012 | Web solutions

Here is a quick how-to for a strategy to prevent duplicate content across domains and subdomains of the same site by "whitelisting" discrete domains and redirecting all other requests to the main domain.

Often during a site migration, you add some new subdomains to your virtual ServerAlias configuration. For other reasons, such as supporting "yoursite.ca", "yoursite.com", "yoursite.biz", etc. you add more ServerAlias configurations. To ensure that you don't have issues with duplicate content, you end up creating a bunch of individual rewrite rules such as:

RewriteCond %{HTTP_HOST} ^yoursite\.ca$ [NC]
RewriteRule ^(.*)$ http://yoursite.com$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(new)\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com$1 [R=301,L] 

This "blacklist" approach can get messy and reactive, as you add new rules whenever someone tells you that your site is being indexed under another domain that you didn't even think of.

A "whitelist" approach is a bit cleaner, as you specify which domains you want content to be served under, and redirect everything else:

RewriteEngine On
# Allow these subdomains
RewriteCond %{HTTP_HOST} !^(admin|editorial)\.yoursite\.com$ [NC]
# Don't cause a rewrite loop
RewriteCond %{HTTP_HOST} !^yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com$1 [R=301,L]

This works very well for single-domain websites, and you are usually subject to fewer surprises in terms of domains that are pulling up your site content.

If you run a multi-site install where, for example, you're running "yoursite.com" off one subtree and "othersite.com" off another subtree, you will have to make the rewrite conditions above more sophisticated. In such a case, however, the better solution might be to create separate vhost files anyway.