.htaccess Redirections

I searched for information on .htaccess redirection but couldn't find anything, this is what I'm looking for.

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

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

      

and this will redirect them:

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

      

So basically accept any subdomain and automatically redirect to a folder at the root of the domain with the name of that subdomain.

+1


source to share


2 answers


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 are not saying what should happen to http://foo.example.com/bar?moo - I went to http://example.com/foo/bar?moo Change the last line if that's not what you want ...

+2


source


If you just want them to be the input:

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

      



Otherwise:

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

      

0


source







All Articles