问题
I have googled all day long and I cant get this right. I have the following rule in my htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{HTTP_HOST} !^app\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://app.domain.com/site/%1/$1 [L]
What this code does is as following
Visit: subsite.domain.com/contact-us It will redirect you to app.domain.com/site/subsite/contact-us
This is what I want BUT i dont want an actual redirect. So when I browse this "subsite" I always wanna have subsite.domain.com/[something...]
I have read that removing the domain name from the RewriteRule will fix it. But it doesnt work...
Is this possible with just htaccess rules?
回答1:
So the only solution to this to add new entires for each subdomain in the vhost file?
That's not entirely accurate.
You can create wildcard subdomains so that any subdomain will be accepted.
In DNS for your domain name add a subdomain called *
.
*.example.com 192.168.1.1
Then in your apache vhost file below ServerName
add a new serveralias
line or add it to the existing serveralias
.
ServerAlias *.domain.com
Then in your .htaccess file try using a proxy using the P flag
because your can't use a different domain name in rewrite or it will do a redirect as you have found out. Ensure mod_proxy
is enabled in apache. It usually is. Then your app can handle the requested folders.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteCond %{HTTP_HOST} !^app\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://app.domain.com/site/%1/$1 [P]
来源:https://stackoverflow.com/questions/32486597/subdomain-point-to-subfolder-without-redirect