views:

267

answers:

4

I've had some users trying to access a site that is registered as subdomain.example.com with www.subdomain.example.com.

is there some sort of .htaccess rule I can add to redirect people that arrive using www.subdomain.example.com to subdomain.example.com?

Also, do I have to change DNS stuff?

+1  A: 
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com
RewriteRule (.*) http://subdomain.domain.com/$1 [R=301,L]
Davinel
Using the rewrite engine is a pretty heavyweight way to solve this problem. The `Redirect` directive does exactly the same thing.
Greg Hewgill
+4  A: 

Sure, use a directive like:

<VirtualHost *:80> 
    ServerName www.subdomain.example.com 
    Redirect permanent / http://subdomain.example.com/ 
</VirtualHost> 

Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).

Also, yes you will need to change DNS records, because www.subdomain.example.com is a distinct hostname that needs its own A (or CNAME) record to point the browser to an appropriate server in the first place.

Greg Hewgill
this assumes the OP is using `NameVirtualHost *:80`
Martin
@Martin: That's a reasonably safe assumption. If not, then the important part is the `Redirect` directive, not the `VirtualHost` container.
Greg Hewgill
The fact that people are already accessing the site using `www.subdomain.domain.com` suggests that that DNS record may already be in place... if so, it doesn't need to be changed.
David Zaslavsky
A: 

You need to add a virtual host directive in httpd.conf and Redirect Permament to the correct subdomain and add the additional DNS entry (CNAME is fine)

Geek Num 88
A: 

Is it possible to use a wildcard in subdomain (www.*.domain.com)?

adi