Subdomain point to subfolder (WITHOUT REDIRECT)

断了今生、忘了曾经 提交于 2019-12-25 07:06:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!