MOD_REWRITE 的行为不符合预期

发布于 2024-12-28 21:39:33 字数 650 浏览 1 评论 0原文

这不起作用:

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 技术交流群。

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

发布评论

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

评论(1

故事↓在人 2025-01-04 21:39:33

通过使用 $,您可以指定它是文本的结尾,因此不会匹配 / 之后的任何内容。 (在正则表达式中,^ 指定字符串的开头,$ 指定字符串的结尾)

您可以删除 $,然后它将生成第二个参数可选 - 这听起来像是您正在寻找的。

试试这个看看你的 mod_rewrite 是否正常工作:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [R,L]

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:

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