如何解决 .htaccess mod_rewrite 和尾部斜杠的问题
我使用 .htaccess mod_rewrite
来获得更好的 URL。
这是我的 .htaccess 文件的一部分,它将 /artist.php?id=123456
转换为 /artist/123456
、/artists.php?culture=arabic
到 /artists/arabic
中,并删除 .php:
RewriteRule ^artist/([0-9]+) artist.php?id=$1
RewriteRule ^artists/(.+) artists.php?culture=$1
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
它可以工作,但是如果我在末尾添加一个尾部斜杠 /
/artist/arabic/
它会抛出 404 错误。我该如何解决这个问题?简单地删除尾部斜杠可能会起作用,但我不知道如何做到这一点。我只是不希望用户意外添加 /
或其他内容,然后感到困惑,认为该页面不存在。
提前致谢!
I'm using .htaccess mod_rewrite
for nicer URLs.
Here is a part of my .htaccess file that turns /artist.php?id=123456
into /artist/123456
, /artists.php?culture=arabic
into /artists/arabic
, and removes the .php:
RewriteRule ^artist/([0-9]+) artist.php?id=$1
RewriteRule ^artists/(.+) artists.php?culture=$1
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It works, but if I put a trailing slash /
at the end like /artist/arabic/
it throws a 404 error. How can I fix this? Simply removing the trailing slash would probably work but I don't know how to do that with this. I just don't want a user to accidentally add a /
or something and then get confused thinking the page doesn't exist.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需使用
/?
在第一条规则中添加可选的尾部斜杠即可You just need to add an optional trailing slash into the first rule with
/?