views:

461

answers:

3

I'm using LAMP server, and I need to redirect requests like:

www.www.example.com to www.example.com and other variations like (wwww.example.com, etc)

I know I can do it in .htaccess, but I don't know the regular expression that I should use to represent all these possibilities.

Or there is any diferent approach, comming from the vhosts?

+5  A: 

I like:

# force www IN URL
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# END force www IN URL

Basically anything that's not www.example.com will get redirect 301'd.

artlung
+2  A: 

See this article for implementing "wildcard subdomains." You're going to need to implement both an .htaccess and modifying the Vhosts.

http://www.easymodrewrite.com/example-subdomains

If you're interested in only limiting it to "www.www," "wwww.," and the other examples above, you can easily do so with some server-side coding (which makes it more flexible than implementing more .htaccess code). Just detect what the subdomain is, and redirect where you want.

James Skidmore
A: 

Using name based virtual hosts in Apache you can do this:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias wwww.example.com www.www.example.com [space seperated list]

    Redirect / http://www.example.com/
</VirtualHost>

Redirect can take a parameter to specify the type of redirect.

Cogsy
Downside of this approach is you have to know the typos in advance.
artlung
you still need all the possible 'typos' in the DNS...
Cogsy