MOD_REWRITE 的行为不符合预期
这不起作用:
RewriteRule ^([^/]+)([/]?)$ /index.cgi?l=$1 [NC,L]
这不起作用:
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
.htaccess 文件中没有其他规则。这是完整的版本:
Options -Indexes
Options ExecCGI
AddHandler cgi-script .cgi .pl .q
ErrorDocument 500 /error500.cgi
ErrorDocument 404 /error404.cgi
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
这确实有效,但这不是我想要的。
RewriteRule ^([^/]+)/([^/]+)$ /index.cgi?l=$1&a=$2 [NC,L]
我希望第一个斜杠和第二个目录都是可选的。为什么问号不能像预期那样匹配 0 或 1 个实例?我这里吓坏了...
This doesn't work:
RewriteRule ^([^/]+)([/]?)$ /index.cgi?l=$1 [NC,L]
This doesn't work:
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
There's no other rules in the .htaccess file. Here's the complete version:
Options -Indexes
Options ExecCGI
AddHandler cgi-script .cgi .pl .q
ErrorDocument 500 /error500.cgi
ErrorDocument 404 /error404.cgi
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
This DOES work, but it's not what I want.
RewriteRule ^([^/]+)/([^/]+)$ /index.cgi?l=$1&a=$2 [NC,L]
I want both the first slash and second directory to be optional. Why won't the question mark match 0 or 1 instances like it's supposed to? I am freaking here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
$
,您可以指定它是文本的结尾,因此不会匹配/
之后的任何内容。 (在正则表达式中,^
指定字符串的开头,$
指定字符串的结尾)您可以删除
$
,然后它将生成第二个参数可选 - 这听起来像是您正在寻找的。试试这个看看你的 mod_rewrite 是否正常工作:
By using a
$
, you're specifying that it's the end of the text, so that won't match anything after a/
. (in regex,^
specifies the beginning of a string and$
specifies the end)You could remove the
$
, then it will make the second parameter optional - that sounds like what you're looking for.Try this to see if your mod_rewrite is working correctly: