Drupal .htaccess rewrite with 'www' but not for sub-domains

Say you have your Drupal's .htaccess set "To redirect all users to access the site WITH the 'www.' prefix", and you added a sub-domain, and it came out looking something like http://www.sub.domain.com. So, most people will NOT want the "www" added to the url in this case. So what do you do? Easy.

Use this instead:


# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|subdomain3)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

So what changed? We simply added this line:


RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|subdomain3)\. [NC]

What that does is say, if you have the following subdomains, do NOT add the 'www'.

(and yeah, you would replace subdomain1, subdomain2, subdomain3, etc with an actual sub-domain name)

Example:


# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^(clients|forum|wackadoodle)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

So there we have it, a way not to add 'www' for subdomain urls.

Additional stuff

If you're using Drupal's Domain Access and on DirectAdmin, you'll probably want to check out how to create sub-domains the correct way by following this DirectAdmin tutorial on creating subdomains in the parent directory, instead of a sub-directory.

Tags: Drupal Linux admin