MDeuerlein MDeuerlein - 2 years ago 136
PHP Question

.htaccess rewrite subdomain to directory and keep subdomain in url

I need to rewrite a subdomain to a subdirectory using .htaccess but keep the subdomain in the url like this:

Visited url in browser:

Served url:

I tried the following

RewriteCond %{HTTP_HOST} ^
RewriteRule ^(.*)$$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?

Answer Source

You can use this rule in document root:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download