重写规则不起作用
这是我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将尝试查找目录
/1234/
并失败。将/
更改为-
,它应该可以工作编辑:完全错误......实际上你在开头有一个
/
模式的,而 MOD_REWRITE 接收没有第一个斜杠的路径。it will be trying to find the directory
/1234/
and failing. change the/
to a-
and it should workEDIT: 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.