views:

117

answers:

1

There is an answer to the question of handling wild card subdomains, but it does not meet all my needs. I need to do all of these, supposing for example that the domain is example.com:

  1. If the domain given is www.example.com it should 301 redirect to example.com because I do not want to have duplicate content that will create problems in search engines.

  2. If the domain is anythingelse.example.com it should silently redirect to example.com so that software can pick up what subdomain was requested and act accordingly.

  3. In addition, any specification after the domain name that does not refer to an actual file or directory should be redirected to index.php (again the software will figure out what it all meant). So anything.example.com/some/search/engine/friendly/specification should redirect to example.com/index.php.

I can get parts of this to work, but have yet to achieve the whole thing. Any offers!

A: 

Try these rules:

# 1. rule
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^ http%1://example.com%{REQUEST_URI} [L,R=301]

# 2. rule
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /absolute/filesystem/path/to/example.com%{REQUEST_FILENAME} [L]

# 3. rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Gumbo