【问题标题】:htaccess redirect if the url does not contain a specific directory如果 url 不包含特定目录,则 htaccess 重定向
【发布时间】:2014-03-08 15:45:36
【问题描述】:

使用 htaccess,我需要将所有不包含特定 URL 路径(医疗保健)的 URL 重定向到新域,同时保持原始 URL 不变。

例子:

healthcare.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 相同的根。他们希望能够在healthcare.domain.com 访问以下页面staging.domain.com/industries/healthcare/。我将healthcare.domain.com 设置为一个新的子域,将其根指向与staging.domain.com 相同的根。然后,我在healthcare.domain.com 的htaccess 中设置了一个重定向,以重定向到healthcare.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

【问题讨论】:

  • healthcare.domain.comstaging.domain.com 是否在同一主机上和同一 VirtualHost 上?
  • 我在上面的问题中添加了更多内容来回答你的问题。
  • staging.domain.com/industries/healthcare/ 是否与healthcare.domain.com/industries/healthcare/ 相同?
  • 你有没有想过为什么http://staging.domain.com/whatever总是重定向到http://staging.domain.com/index.php?我也面临同样的问题。

标签: .htaccess mod-rewrite redirect


【解决方案1】:

试试:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R]

【讨论】:

  • 我有一个与你的几乎相同的,它返回了相同的结果 - 它只是给了我 staging.domain.com/index.php。我在上面的问题中添加了更多信息,可能会更清楚地说明原因。
【解决方案2】:

您可以使用此代码:

RewriteCond %{HTTP_HOST} ^(www\.)?healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/ [NC]
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R=302]

RewriteCond %{HTTP_HOST} ^(www\.)?healthcare\.domain\.com$ [NC]
RewriteRule ^/?$ http://healthcare.domain.com/industries/healthcare/ [R=302,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多