如果 url 不包含特定目录,则 htaccess 重定向 [英] htaccess redirect if the url does not contain a specific directory

查看:38
本文介绍了如果 url 不包含特定目录,则 htaccess 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With htaccess I need to redirect all URLs that don't contain a specific URL path (healthcare) to a new domain while keeping the original URL intact.

示例:

healthcare.domain.com/anydirectory/anything.html

healthcare.domain.com/anydirectory/anything.html

应该重定向到

staging.domain.com/anydirectory/anything.html

staging.domain.com/anydirectory/anything.html

但是任何包含/healthcare 的 URL 都不应该被重定向.正如在healthcare.domain.com/industries/healthcare/customers.html 中一样,不应重定向.

but any URL containing /healthcare should not get redirected. As in healthcare.domain.com/industries/healthcare/customers.html should not be redirected.

两个域都在同一台服务器上,且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

Both domains are on the same server and healthcare.domain.com is pointed to same root as staging.domain.com. They wanted to be able to access the following page staging.domain.com/industries/healthcare/ at healthcare.domain.com. I setup healthcare.domain.com as a new subdomain pointing its root to the same root as staging.domain.com. I then setup a redirect in the htaccess for healthcare.domain.com to redirect to healthcare.domain.com/industries/healthcare which works perfectly, but if you click anywhere else on the site outside of /industries/healthcare/ I need to bring the user back to 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]

这篇关于如果 url 不包含特定目录,则 htaccess 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆