How can I use “mod rewrite” to redirect a folder path to a subdomain, but without a browser redirect?

烈酒焚心 提交于 2019-12-21 20:45:16

问题


I have a file that I want to redirect to a subdomain using mod_rewrite. For example, I would like to redirect the request for http://www.mydomain.com/xyz to the subdomain xyz.example.com

I don't want to send a redirect to the browser though (so it doesn't know the page is different).

BTW. xyz is a CNAME record, pointing to www.otherdomain.com.

Another example, just to clarify. If http://www.mydomain.com/xyz/something is entered into the browser, I want Apache to return the contents of http://xyz.mydomain.com/something


回答1:


I THINK this will do what you want it to:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)/(.*?)$ http://$1.mydomain.com/$2

As this stands it will redirect EVERY request that goes to a subfolder so if you links for images or css that are in a sub folder it will also rewrite those, if you want to list only certain things you could use this

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(blog|store|other)/(.*?)$ http://$1.mydomain.com/$2

This will only redirect mydomain.com/blog/ mydomain.com/store/ and mydomain.com/other/




回答2:


If both domains are on the same machine and accessible over the filesystem, you should go this way. For example:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^([^/]+)/(.*) /absolute/path/to/subdomains/$1/$2

If this is not the case, you’ll need a proxy:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^([^/]+)/(.*) http://$1.example.com/$2 [P]

These examples are inteded for a .htaccess file. If you want to use them in the httpd.conf, prefix the RewriteRule patterns with a /.



来源:https://stackoverflow.com/questions/597390/how-can-i-use-mod-rewrite-to-redirect-a-folder-path-to-a-subdomain-but-withou

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