Warm tip: This article is reproduced from serverfault.com, please click

.htaccess-htaccess重定向特定页面,而不是整个域

(.htaccess - htaccess redirection of a specific page, not whole domain)

发布于 2020-11-30 11:40:19

我需要使用htaccess页面(不是整个域!)重定向到另一个域上的特定其他页面。我尝试了几件事,但无法使其正常工作。有人可以帮我吗?

我尝试了这个:

#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^domain.com/example [NC]
#RewriteRule ^(.*) https://otherdomain.com/page [P]

还有这个:

#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^www.domain.com/example/$ [NC]
#RewriteRule ^(.*)$ http://www.mainpage.com/pagename/$1 [L,R=301,NC]

应该重定向的页面是/ example

domain.com/example >>>应该重定向到:https : //otherdomain.com/page

使用和不使用www均可访问应定向的页面。在它前面,不确定这是否相关。

非常感谢!

Questioner
Vincentin
Viewed
0
Amit Verma 2020-11-30 21:52:14

你的规则无效,因为它在 RewriteCond

#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]

由于%{HTTP_HOST}variabe仅包含主机标头,即,永远不会满足此条件example.com而不是URI字符串/example/你可以%{REQUEST_URI}在单独的变量中使用变量测试URI字符串RewriteCond

这应该为你工作

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}  /example [NC]
RewriteRule ^ https://otherdomain.com/page [R,L]