简体   繁体   English

使用Apache,如何将网站的子文件夹重定向到子域,但将除该子文件夹之外的所有内容都重定向到主域?

[英]Using Apache, how can I redirect a subfolder of a site to a subdomain but redirect everything but that subfolder to the primary domain?

I have the same website running on Apache at two different subdomains using vhosts. 我使用vhost在两个不同的子域上的Apache上运行相同的网站。

www.vhost1.com

www.vhost2.com

Whenever someone accesses a particular subfolder on www.vhost1.com , I want the user to be redirected to that same subfolder on www.vhost2.com . 每当有人访问www.vhost1.com上的特定子文件夹时,我都希望用户被重定向到www.vhost2.com上的同一子文件夹。

Example: User accesses www.vhost1.com/my-special-folder User should be redirected to www.vhost2.com/my-special-folder 示例:用户访问www.vhost1.com/my-special-folder用户应重定向到www.vhost2.com/my-special-folder

And whenever someone is on www.vhost2.com and accesses anything except that subfolder, I want the user to be redirected to www.vhost1.com/any-other-url 每当有人访问www.vhost2.com并访问该子文件夹以外的任何内容时,我都希望用户重定向到www.vhost1.com/any-other-url

Example: User accesses www.vhost2.com/any-other-url User should be redirected to www.vhost1.com/any-other-url 示例:用户访问www.vhost2.com/any-other-url用户应重定向到www.vhost1.com/any-other-url

Also, if the user accesses www.vhost2.com (with no path), the user should be redirected to www.vhost1.com 另外,如果用户访问www.vhost2.com (无路径),则应将用户重定向到www.vhost1.com

Example: User accesses www.vhost2.com User should be redirected to www.vhost1.com 示例:用户访问www.vhost2.com应该将用户重定向到www.vhost1.com

I've tried several different Apache redirect rules and have gotten several of the scenarios to work, but I can't get them all to work at the same time. 我已经尝试了几种不同的Apache重定向规则,并获得了一些可行的方案,但是我无法同时使它们全部起作用。

Because of the server configuration, I must perform the redirects using a .htaccess file. 由于服务器的配置,我必须使用.htaccess文件执行重定向。

What should the Apache rules be? Apache规则应该是什么?

Place this rule in root .htaccess of both domains: 将此规则放在两个域的根.htaccess中:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?vhost1\.com$ [NC]
RewriteRule ^(my-special-folder)(/.*)?$ http://www.vhost2.com/$1$2 [L,NC,NE.R=302]

RewriteCond %{HTTP_HOST} ^(www\.)?vhost2\.com$ [NC]
RewriteRule ^((?!my-special-folder/).*)$ http://www.vhost1.com/$1 [L,NC,NE,R=302]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM