views:

23

answers:

4

I have the problem but with many subdomains: E.g..

sub1.domain.com and new.domain.com and xsub.domain.com and many many more like this.

How do I remove the www from in front of any of these with one generic rule.

E.g. if someone types www..domain.com or http://www..domain.com to change it to

http://.domain.com

Thanks

A: 

You may use the rewrite module to remove the www. when it precedes a sub-domain. In this way, an address like: www.sub1.domain.com would be redirected to sub1.domain.com:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteBase /
   RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
   RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
   RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
</IfModule>
orvado
A: 

Is this the right way to add it to my .htaccess (its drupal). Or do I need to remove the [L] in the first rewrite rule. -- thanks

<IfModule mod_rewrite.c>

  RewriteEngine on

### ADDED
   RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
   RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
   RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
####

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</IfModule>
wyattbiker
A: 

Also would be nice to make the first RewriteCond generic so the domain name will not matter.

E.g would this work (If it is not a domain of the form www.nnnnnnn.sss)

So now the only question would how the RewriteRule is it still %1 or now it's %3?

### ADDED
   RewriteCond %{HTTP_HOST} !^www\.([^\..]*)\.([^\..]*)$ [NC]
   RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
   RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
####
wyattbiker
A: 

Modified tested solution but for http only.

#Allow domain of the form www.domain.com
RewriteCond %{HTTP_HOST} !^www\.([^\..]*)\.([^\..]*)$ [NC]

#Otherwise any other form must be rewritten to remove www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

#Substitue the complete domain using group %1 in the parentheses of the above condition
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
wyattbiker