.htaccess重定向,无重定向循环 [英] .htaccess redirect without redirect loop
问题描述
我肯定在那里有一个答案,但是它确实让我耳目一新,并且我讨厌与.htaccess重定向有关的任何事情.
Im sure there is an answer out there for this somewhere but its doing my head in and I hate anything to do with .htaccess redirects.
基本上我正在尝试这样做
Basically I am trying to do this
RewriteRule /about/(.*) http://www.domain.com/example/about/$1 [R=301,L].
问题在于,这导致重定向循环,因为从URL重定向到URL都包含"about".
Problem is that this results in a redirect loop because "about" is in both the redirected from and redirected to url.
id想要做的是拥有它,以便仅当根URL在/about/之前时才重定向.我确实尝试过这样做:
What id like to do is have it so that it will only redirect if the root url is before the /about/. I did try doing this with this:
RewriteRule http://www.domain.com/about/(.*) http://www.domain.com/example/about/$1 [R=301,L].
但是那没用.
有人对此有直接解决方案吗?
Does anyone have a straight forward solution for this.
谢谢
推荐答案
此规则应使用正则表达式模式中的锚点:
This rule should work with anchors in regex pattern:
RewriteRule ^/?(about/.*)$ /example/$1 [R=301,L,NC]
^/?about
仅匹配http://www.domain.com/about/
,而不匹配http://www.domain.com/example/about/
这篇关于.htaccess重定向,无重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!