我需要使用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均可访问应定向的页面。在它前面,不确定这是否相关。
非常感谢!
你的规则无效,因为它在 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]
非常感谢,它有效!!只是想知道,但是在您没有看到url更改的情况下,通过重定向也可以做到这一点吗?
@Vincentin这可以通过
P
代理标志来实现。如果代理模块在服务器上enebled则只需更换[R,L]
与[P]
上面的重写规则。如果未启用该模块,则会收到500服务器错误。谢谢!不幸的是,这不起作用,但是也许我可以启用该模块。非常感谢!:)