Subdomains and mod_rewrite (remove www.)

StijnH

New member
Joined
May 25, 2009
Messages
4
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Above code should remove 'www.' from all domains and subdomains. It is listed in the mod_rewrite FAQ and various other resources.

However, it does not work as expected with DirectAdmin. While trying to find a solution, I've read that you have a 'special' way of handling subdomains, which probably causes this problem.
I cannot add more domains to my hosting package, so the alternative way of adding subdomains is not possible.

http://domain.tld/ stays http://domain.tld/
http://www.domain.tld/ goes to http://domain.tld/

http://sub.domain.tld/ stays http://sub.domain.tld/
WRONG:
http://www.sub.domain.tld/ goes to http://sub.domain.tld/sub/
THIS SHOULD HAPPEN:
http://www.sub.domain.tld/ goes http://sub.domain.tld/

Any help will be appreciated!
 
Place this in the .htaccess file located in the webroot directory (/home/domain/domains/domain.tld/public_html):

Code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{HTTP_HOST} !^www\.domain\.tld
  RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L] 
  RewriteCond %{ENV:REDIRECT_STATUS} 200
  RewriteRule .* - [L]
</IfModule>


Place this in your .htaccess file located in the subdomain's root directory (/home/domain/domains/domain.tld/public_html/sub):


Code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{HTTP_HOST} !^sub\.
  RewriteRule ^(.*)$ http://sub.domain.tld/$1 [R=301,L] 
  RewriteCond %{ENV:REDIRECT_STATUS} 200
  RewriteRule .* - [L]
</IfModule>

Let us know if it worked. :)
 
Hello, thank you for your reply :)
The code you posted is not what I want (it forces www. instead of removing it), but I understand the principe of it: having a .htaccess for the domain and each subdomain.
It's a workaround I've considered using, though the original code still doesn't behave like it should.
 
The code you posted is not what I want (it forces www. instead of removing it)

Oops! My apologies, I wasn't paying attention. Normally people want to force the www, not remove it (think SEO and organic links: people are more likely to link to the www. version, regardless what you have set).

Here's code to remove the www (for .htaccess in domain.tld webroot):

Code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{HTTP_HOST} ^www\.domain\.tld
  RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L] 
  RewriteCond %{ENV:REDIRECT_STATUS} 200
  RewriteRule .* - [L]
</IfModule>


but I understand the principe of it: having a .htaccess for the domain and each subdomain. It's a workaround I've considered using, though the original code still doesn't behave like it should.

Subdomains have their own VirtualHost entries, so even if they share the domain.tld part and are located in a subdirectory, they are their "own entities" with their own webroot. That's why I wouldn't call domain.tld and sub.domain.tld having their own .htaccess rules exactly a workaround.
 
Back
Top