Jonas Stevens
Jonas Stevens

Reputation:

.htaccess Redirections

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.

Upvotes: 1

Views: 456

Answers (2)

Gumbo
Gumbo

Reputation: 655707

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]

Upvotes: 0

Greg
Greg

Reputation: 321806

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.

Upvotes: 2

Related Questions