问题
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