I need to rewrite a subdomain to a subdirectory using .htaccess but keep the subdomain in the url like this:
Visited url in browser: sub1.domain.com
Served url: sub.domain.com/sub1/
I tried the following
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteRule ^(.*)$ http://subdomain.domain.com/subdomain/$1 [L,NC,QSA]
but this leads to an endless loop. I know that it would be easier to do this by php but is there any solution for this with using .htaccess?
You can use this rule in document root:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC]
Explanation:
NC
: Ignore caseL
: Last ruleRewriteCond %{HTTP_HOST}
line makes sure that rule only executes for subdomain
(?!sub1/)
is negative lookahead expression that means of request is not starting with /sub1/
RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC]
rewrites current path to /sub1/<uri>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With