重写规则不起作用

发布于 2024-11-07 22:21:11 字数 492 浏览 0 评论 0原文

这是我的 .htaccess 文件的内容:

RewriteEngine on
RewriteRule ^upload$ upload.php
RewriteRule ^/(\d+)/?.*$ /view.php?id=$1 [L]

第一条规则成功运行。当我导航到 http://localhost/upload 时,它显示 upload.php 页面。

然而,第二条规则则不然。当我浏览到:http://localhost/1234/some-string 时,出现 404 错误。它的目的是显示此页面:http://localhost/view.php?id=1234

希望您能明白我想对规则做什么,我希望完全忽略 URL 末尾的最后一个字符串,并将 1234 作为 view.php 的参数。

有人能发现为什么这不起作用吗?我已经尝试了我能想到的一切,但没有成功。谢谢!

This is the contents of my .htaccess file:

RewriteEngine on
RewriteRule ^upload$ upload.php
RewriteRule ^/(\d+)/?.*$ /view.php?id=$1 [L]

The first rule successfully works. When I navigate to http://localhost/upload it shows the upload.php page.

The second rule however, does not. When I browse to: http://localhost/1234/some-string I get a 404 error. It's meant to show this page: http://localhost/view.php?id=1234.

Hopefully you can see what I'm trying to do with the rule, I want the last string on the end of the URL to be completely ignored, and take the 1234 as a parameter for view.php.

Can anyone spot why this isn't working? I've tried everything I can think of, but to no success. Thanks!

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

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

发布评论

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

评论(1

血之狂魔 2024-11-14 22:21:11

它将尝试查找目录 /1234/ 并失败。将 / 更改为 - ,它应该可以工作

编辑:完全错误......实际上你在开头有一个 /模式的,而 MOD_REWRITE 接收没有第一个斜杠的路径。

it will be trying to find the directory /1234/ and failing. change the / to a - and it should work

EDIT: got that completely wrong ... it's actually that you have a / at the beginning of your pattern, whereas MOD_REWRITE receives the path without the first slash.

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