views:

177

answers:

2

Hi, I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.

Basically, I want a solution that will take a site example.com and allow you to enter URL's like:

123.example.com ksdfkjds.example.com dsf38jif348.example.com

and this would redirect them to:

example.com/123 example.com/ksdfkjds example.com/dsf38jif348

So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain. Thanks for your help!

+2  A: 

Try something like this:

# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com

# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]

# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]

You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo Change the last line if that's not what you want.

Greg
A: 

If you just want them to be the entrance:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]

Otherwise:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]
Gumbo