Apache 2.0 RewriteRule 正则表达式中不可避免的时期
因此,我有以下正则表达式。
RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]
我想要 file.php
触发的 URL 必须包含大写字母或句点,然后将该 URL 发送到 PHP 脚本。
但是,我遇到的问题是,由于未真正转义的句点,该脚本会在任何 URL 上触发。
我尝试过用一个反斜杠、两个反斜杠或三个反斜杠转义句点……但都无法阻止通用解释。
我做错了什么?
编辑:例如,
RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
不起作用,但
RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
可以转义它。
编辑2:我想要重定向的网址示例:
- /some-page-goes-here.html
- /heres-Robs/Old/Page/
以及我不想重定向的网址:
- /testing/one/two //an/actual-file.gif
- 。
编辑 3: 旧的正则表达式是:
RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
但是在撰写帖子时,我将问题的正则表达式更新为您在上面看到的内容
So, I have the following RegEx..
RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]
The URL I want file.php
to trigger for must either have capital letters or a period in it, then send the URL to the PHP script.
However, the problem I have is that this script is triggering on any URL, because of the not-truly-escaped Period.
I've tried escaping the period with a backslash, or two backslashes, or three... but none stop the generic interpretation.
What am I doing wrong?
Edit: As an example,
RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
Doesn't work, but
RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
does escape it.
Edit 2: Examples of URLs I want to redirect:
- /some-page-goes-here.html
- /heres-Robs/Old/Page/
And ones I don't:
- /testing/one/two/
- /an/actual-file.gif
EDIT 3: Old regex was:
RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
But while writing the post, I updated the question's regex to what you see above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
当使用 mod_rewrite 并且您有多个要匹配的 URL 时,最好使用
RewriteCond
进行过滤,然后应用您的RewriteRule
。Try:
When using mod_rewrite and you have several URLs to match, it is always better to use
RewriteCond
to filter and then apply yourRewriteRule
.我认为您的问题可能不是您所想的那样:字符类中的句点应该表示字面句点,而不是“任何字符”。如果这确实是问题所在,那么您可以将
[AZ\.]+
更改为([AZ]|\.)+
;但我对此表示怀疑。需要尝试的一些事情:此外,您的规则不会不太符合你的描述。例如,您的规则不会匹配像
abc
这样的 URL,因为您只在单个“块”中出现大写字母和/或点;如果它们以小写字母分隔,则不会发生匹配。这只是因为您不想使描述过于复杂吗?I don't think your problem can be what you think it is: periods in a character class are supposed to mean literal periods, not "any character". If this really is the problem, somehow, then you could change
[A-Z\.]+
to([A-Z]|\.)+
; but I doubt it. Some things to try:QSA
toQSA,R
? Does the destination URL look like what you expect? Maybe there are some unexpected periods or uppercase letters? (Warning: this will very likely trigger an infinite redirect loop if you try it in a browser; it'll probably be easier to try submitting the request via port-80 Telnet and seeing the actual HTTP response.)Also, your rule doesn't quite match how you describe it. For example, your rule wouldn't match a URL like
a.b.c
, because you only uppercase letters and/or dots to occur in a single "clump"; if they're separated by lowercase letters, no match will occur. Is that just because you didn't want to overcomplicate the description?