如何解决 .htaccess mod_rewrite 和尾部斜杠的问题

发布于 2025-01-04 17:15:29 字数 628 浏览 3 评论 0原文

我使用 .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 技术交流群。

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

发布评论

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

评论(1

自我难过 2025-01-11 17:15:29

您只需使用 /? 在第一条规则中添加可选的尾部斜杠即可

# Optional trailing slash...
RewriteRule ^artist/([0-9]+)/? artist.php?id=$1
#--------------------------^^^^
RewriteRule ^artists/(.+) artists.php?culture=$1
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

You just need to add an optional trailing slash into the first rule with /?

# Optional trailing slash...
RewriteRule ^artist/([0-9]+)/? artist.php?id=$1
#--------------------------^^^^
RewriteRule ^artists/(.+) artists.php?culture=$1
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文