另一个 mod_rewrite 尾部斜杠问题

发布于 2024-07-13 20:37:06 字数 963 浏览 9 评论 0原文

我的 .htaccess 文件中有以下重写规则:

RewriteEngine on
RewriteRule ^news/([0-9]+)/?$ /?action=news&start=$1 [L]
RewriteRule ^man/([a-zA-Z0-9_]+)/?$ /?action=man&page=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/?$ /?action=$1 [L]

所有这些都以 /? 结尾,以检查可选的尾部斜杠。 这有效 - 这意味着我可以使用 http://www.site.com/news/http://www.site.com/news 来访问news 页面,这就是我想要的。 问题是,虽然 http://www.site.com/news/ 工作正常并且静默重定向以及所有有趣的东西,http://www.site.com/news由于某种原因, 明显重定向到 http://www.site.com/news/?action=news。 他们都提出了相同的网站,但由于某种原因,如果我省略尾部斜杠,URL 就会发生变化并且看起来很糟糕。

有什么想法吗? 我的 .hyaccess 文件中没有其他规则。 我还要指出 http://www.site.com/news/0http://www.site.com/news/0/ 确实不会遇到同样的问题。 它们都无形地重定向到 http://www.site.com/?action=news&start=0,这就是我想要的。 这似乎只是第三条规则的问题。

如果有帮助的话,该站点位于 SourceForge.net 上。

I have the following rewrite rules in my .htaccess file:

RewriteEngine on
RewriteRule ^news/([0-9]+)/?$ /?action=news&start=$1 [L]
RewriteRule ^man/([a-zA-Z0-9_]+)/?$ /?action=man&page=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/?$ /?action=$1 [L]

All of then end with a /? to check for an optional trailing slash. This works - it means I can use either http://www.site.com/news/ or http://www.site.com/news to get to the news page, which is what I want. The problem is that, while http://www.site.com/news/ works fine and redirects silently and all that fun stuff, http://www.site.com/news is visibly redirected to http://www.site.com/news/?action=news for some reason. They both come up with the same site, but for some reason if I leave off the trailing slash, the URL changes and looks all nasty.

Any ideas what's going on with this? I have no other rules in my .hyaccess file. I'll also point out that http://www.site.com/news/0 and http://www.site.com/news/0/ do not suffer from the same problem. Both of them redirect invisibly to http://www.site.com/?action=news&start=0, which is what I want. It only seems to be a problem with that third rule.

If it helps any, the site is on SourceForge.net.

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

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

发布评论

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

评论(2

暮色兮凉城 2024-07-20 20:37:06

一个疯狂的猜测:有一个名为“news”的目录,并且自动 Apache 目录“添加尾部斜杠”重定向正在启动并以一种奇怪的方式与您的 RewriteRule 交互。 如果是这样的话,

DirectorySlash Off

可能会有所帮助。

A wild guess: there is a directory named "news", and the the automatic Apache directory "add a trailing slash" redirect is kicking in and interacting with your RewriteRule in a weird way. If that's the case,

DirectorySlash Off

may help.

若无相欠,怎会相见 2024-07-20 20:37:06

也许 MultiViews 导致了这种情况行为。 尝试禁用此选项:

Options -MultiViews

另一种猜测:删除替换中的前导斜杠。

Maybe MultiViews is causing this behavior. Try to disable this option:

Options -MultiViews

Another guess: remove the leading slash in the substitution.

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