在正则表达式中进行条件重写,而不是使用 RewriteCond
我为重写规则编写了一个正则表达式,其中包含一个负前瞻来替换 rewriteCond 行,因为 WordPress 只接受两个值:pattern ->;替代。
它应该找到 findme.html _here,无论它被要求在哪里: mydomain.com/_here/findme.html
例如
(抱歉,我无法修改将在错误的位置请求 findme.html 的 swf)
因此,考虑到可以请求 findme.html,例如:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
重写应该使它们全部
mydomain.com/ _here/findme.html
因此,我制定了一个重写规则,Wordpress 将接受我,如下所示,
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
因此它只匹配其中不包含“_here”的 URL,以防止额外的重写或循环。
问题是它确实循环。
我错过了什么?
I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您想将
(?!_here)
之前的.*
移动到它之后,因为(?!_here)
是一个负的前瞻,因此它会检查文本_here
是否出现在其后面。你的正则表达式实际上检查的是你的url是否以某个后面没有_here
的字符序列开头,并且_here
是后面没有_here的字符序列
。然后你的规则变成另外,看起来你的模式将排除带有子目录的路径,例如
如果你还想包含这些子目录,则模式应该是
It looks to me like you want to move the
.*
that is before(?!_here)
to after it because(?!_here)
is a negative lookahead, so it check that the text_here
does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by_here
, and_here
is a character sequence not followed by_here
. Then your rule becomesAlso, it looks like your pattern will exclude paths with subdirectories such as
If you also want to include those, the pattern should be