Redirect a subdomain to another domain's subfolder and keep the subdomain's URL

社会主义新天地 提交于 2019-12-11 08:08:54

问题


I'd like to redirect a subdomain, say http://sub.domain.com to another domain's subfolder like https://unrelatedsub.otherdomain.com/domain.com/ as well as everything inside, so that https://unrelatedsub.otherdomain.com/domain.com/u/r/l/file can be accessed by going to http://sub.domain.com/u/r/l/file

Also, if it is possible, the browser's address bar should display the subdomain's url (http://sub.domain.com/u/r/l/file)

While searching for solutions, I came accross this possibility

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*) https://unrelatedsub.otherdomain.com/domain.com/$1 [L,R]

However, in this case, accessing http://sub.domain.com/ redirects to https://unrelatedsub.otherdomain.com/domain.com/sub/ (sub shouldn't be in) and doesn't "hide" the URL.


回答1:


You're using the [R] flag which means external redirect, i.e. the address in the address bar changes. If you have mod_proxy loaded, you can use the [P] flag which causes the apache httpd server to proxy the requests, i.e. the url in the address bar stays.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*) https://unrelatedsub.otherdomain.com/domain.com/$1 [P]


来源:https://stackoverflow.com/questions/25335217/redirect-a-subdomain-to-another-domains-subfolder-and-keep-the-subdomains-url

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