Redirect wildcard subdomains to subdirectory, without changing URL in address bar
I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
This can be achieved in .htaccess
without mod_proxy
provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *
). Add this to your .htaccess
file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
Comments
Post a Comment