在正则表达式中进行条件重写,而不是使用 RewriteCond

发布于 2024-12-08 12:45:24 字数 721 浏览 2 评论 0原文

我为重写规则编写了一个正则表达式,其中包含一个负前瞻来替换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2024-12-15 12:45:24

在我看来,您想将 (?!_here) 之前的 .* 移动到它之后,因为 (?!_here)是一个负的前瞻,因此它会检查文本_here 是否出现在其后面。你的正则表达式实际上检查的是你的url是否以某个后面没有_here的字符序列开头,并且_here是后面没有_here的字符序列。然后你的规则变成

RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]

另外,看起来你的模式将排除带有子目录的路径,例如

mydomain.com/directory/subdirectory/findme.html

如果你还想包含这些子目录,则模式应该是

RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]

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 becomes

RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]

Also, it looks like your pattern will exclude paths with subdirectories such as

mydomain.com/directory/subdirectory/findme.html

If you also want to include those, the pattern should be

RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文