使用 htaccess,我需要将所有不包含特定 URL 路径(医疗保健)的 URL 重定向到新域,同时保持原始 URL 不变。
例子:
health.domain.com/anydirectory/anything.html
应该重定向到
staging.domain.com/anydirectory/anything.html
但是任何包含/healthcare 的 URL 都不应该被重定向。正如在healthcare.domain.com/industries/healthcare/customers.html 中一样,不应重定向。
两个域都在同一台服务器上,并且healthcare.domain.com 指向与staging.domain.com 相同的根。他们希望能够访问以下页面 staging.domain.com/industries/healthcare/athealthcare.domain.com。我将healthcare.domain.com 设置为一个新的子域,将其根指向与staging.domain.com 相同的根。然后,我在 htaccess 中为 healthcare.domain.com 设置了一个重定向,以重定向到 health.domain.com/industries/healthcare,它完美地工作,但是如果您单击网站上的任何其他地方/industries/healthcare/我需要带用户返回 staging.domain.com/whatever
这是我到目前为止所拥有的:
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.healthcare\.domain\.com$
RewriteRule ^/?$ "http\:\/\/healthcare\.domain\.com\/industries\/healthcare\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/ [NC]
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R=302]
但以上总是返回 http://staging.domain.com/index.php
最佳答案
尝试:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R]
关于.htaccess - 如果 url 不包含特定目录,则 htaccess 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691986/