htaccess 文件规则

发布于 2024-11-07 16:45:20 字数 367 浏览 0 评论 0原文

我的 .htaccess 中有这样的规则:

RewriteRule ^art_(.*).htm$ art_item.php?id=$1

并且链接如下: art_1.html => art_item.php?id=1

现在我想添加子链接,如下所示: art_1/5.html => art_item.php?id=1&id2=5

RewriteRule ^art_(.*)/(.*).htm$ art_item.php?id=$1&id=$2

我尝试过,但服务器抛出 404 错误。怎么了?

I have such rule in my .htaccess:

RewriteRule ^art_(.*).htm$ art_item.php?id=$1

And link like this: art_1.html => art_item.php?id=1

Now I want to add sublink which looks like: art_1/5.html => art_item.php?id=1&id2=5

RewriteRule ^art_(.*)/(.*).htm$ art_item.php?id=$1&id=$2

I tried that, but server throws 404 error. What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回忆凄美了谁 2024-11-14 16:45:20

首先,您需要注意规则的顺序。

错误的顺序:

RewriteRule ^art_(.*)\.html$ art_item.php?id=$1
RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2

正确的顺序:

RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2
RewriteRule ^art_(.*)\.html$ art_item.php?id=$1

这是因为 (.*) 也匹配 (.*)/(.*),因为 /. 匹配的任何字符

其次:.htm.html 之间存在差异,因此要么小心,要么只使用 \.html?,它与两者匹配。

First, you need to be careful about the order of the rules.

wrong order:

RewriteRule ^art_(.*)\.html$ art_item.php?id=$1
RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2

correct order:

RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2
RewriteRule ^art_(.*)\.html$ art_item.php?id=$1

This is because (.*) also matches (.*)/(.*), since the / is any character matched by ..

Second: there is a difference between .htm and .html, so either be careful or just use \.html?, which matches both.

千寻… 2024-11-14 16:45:20

假设 id 是数字(最好尽可能精确),以下内容应涵盖它:

RewriteRule ^art_(\d+)\.htm$ art_item.php?id=$1
RewriteRule ^art_(\d+)/(\d+)\.htm$ art_item.php?id=$1&id2=$2

注意: id2 用于第二次捕获

The following should cover it assuming id is numeric (it's best to be as exact as possible):

RewriteRule ^art_(\d+)\.htm$ art_item.php?id=$1
RewriteRule ^art_(\d+)/(\d+)\.htm$ art_item.php?id=$1&id2=$2

Note: id2 for the second capture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文