普通的前pression死循环 [英] Regular expression endless loop
问题描述
我在这台服务器,其中两个域的名称指向。所以在我的.htaccess文件我想作一个简单的规则说沿线的东西:
I have this server where two domain-names are pointed to. So in my .htaccess-file I want to make a simple rule that says something along the line:
如果你来自test.domain.com然后去夹X,如果你来自the-other-sub.domain.com然后去夹Y.这意味着我移动子域之一,所以我可以喜欢做它,所以它重定向到正确的网址(在人们关注的一个深层链接的情况下。例如,如果有人去 http://test.domain.com/path/to /一/页
,他们将被重定向到 /路径/到/一/页
在新的文件夹中。
If you come from test.domain.com then go to folder X, if you come from the-other-sub.domain.com then go to folder Y. And this means that I'm moving one of the subdomains, so I could like to make it so it redirects to the right URL (in case that people are following a deep link. For instance if people go to http://test.domain.com/path/to/a/page
that they will be redirected to /path/to/a/page
in the new folder.
我挣扎这样做,虽然。我不明白的是,为什么是这code:
I'm struggling to do so, though. What I don't get is, why is that this code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ /test/$1 [L,R=301]
叶我,所以如果我去 subdomain.domain.com/abc
,那么浏览器就会送我去 subdomain.domain.com /测试/检验/检验/检验/检验/检验/检验/检验/检验/检测/测试/ ABC
然后抱怨,我有和无限循环。并请没有智能链接到Apache的文档mod_rewrite.c ......我读过它,这是它采取了我。我知道,*意味着匹配0次以上,但我不明白为什么副本的目标串一遍又一遍又一遍......? /测试/
不是在常规的前pression一个变量,是吗?那么,为什么它重复一次吗?
Leaves me, so if I go to subdomain.domain.com/abc
, then the browser will send me to subdomain.domain.com/test/test/test/test/test/test/test/test/test/test/test/abc
and then complaining about that I have and endless loop. And please no smart links to Apache's documentation for mod_rewrite.c... I've read it, and this is where it has taken me. I know that the * means 'match 0 or more times', but I don't get why that copies the destination-string over and over and over...? /test/
isn't a variable in the regular expression, is it? So why does it repeat it?
推荐答案
如果您无条件地重定向到 /测试/...
那么它将继续增加 /测试/
之前重定向的URL也。
If you redirect unconditionally to /test/...
then it will keep adding /test/
before redirected URLs also.
要解决使用这样的规则:
To fix use this rule:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^((?!test/).*)$ /test/$1 [L,R=301,NC]
(?!测试/)
是指添加 /测试/
只有当它已经没有按负先行条件T开始 /测试/
。
(?!test/)
is negative lookahead condition which means add /test/
only if it already doesn't start with /test/
.
这篇关于普通的前pression死循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!