apache mod_rewrite '添加路径信息后缀:'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/439218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:07:44  来源:igfitidea点击:

mod_rewrite 'add path info postfix:'

apachemod-rewrite

提问by Chris J Allen

Why is my mod_rewrite doing this?

为什么我的 mod_rewrite 这样做?

add path info postfix: /home/mobelluk/public_html/about.php -> /home/mobelluk/public_html/about.php/

which results in an unwanted trailing slash on EVERYTHING.

这会导致在所有内容上出现不需要的尾部斜杠。

I have disabled all my .htaccess rules so they're out of the equation.

我已经禁用了我所有的 .htaccess 规则,所以它们不在等式中。

采纳答案by Grant Wagner

Is it possible the new server has mod_dirloaded, with DirectorySlash Onwhere the old one did not and that is leading to this problem?

新服务器是否有可能加载了mod_dir,而DirectorySlash On旧服务器没有加载,这导致了这个问题?

(Note that DirectorySlash Onis the default if mod_diris loaded and nothing is overriding it)

(请注意,这DirectorySlash On是默认的 ifmod_dir已加载并且没有任何内容可以覆盖它)

回答by econstantin

apparently there's been an issue with mod_rewrite re-appending post-fix part in certain cases https://issues.apache.org/bugzilla/show_bug.cgi?id=38642

显然在某些情况下 mod_rewrite 重新附加后修复部分存在问题 https://issues.apache.org/bugzilla/show_bug.cgi?id=38642

The problem:

If multiple RewriteRules within a .htaccess file match, unwanted copies of PATH_INFO may accumulate at the end of the URI.

问题:

如果 .htaccess 文件中的多个 RewriteRules 匹配,则 PATH_INFO 的不需要的副本可能会累积在 URI 的末尾。

If you are on Apache 2.2.12 or later, you can use the DPI flag to prevent this http://httpd.apache.org/docs/2.2/rewrite/flags.html

如果您使用的是 Apache 2.2.12 或更高版本,则可以使用 DPI 标志来防止此 http://httpd.apache.org/docs/2.2/rewrite/flags.html

回答by Scott

In searching for "add path info postfix", this question comes up first and while it eventually did solve my problem, it took me almost 2 hours to understand what was going on. In working on a site, I needed this rewrite:

在搜索“添加路径信息后缀”时,这个问题首先出现,虽然它最终确实解决了我的问题,但我花了将近 2 个小时才明白发生了什么。在网站上工作时,我需要重写:

/resources/band/ -> resources.html?section=band

Accomplished with this mod_rewrite:

完成此 mod_rewrite:

RewriteRule ^resources/(.*)/$ resources.html?section= [L]

Changing that to [DPI] did nothing... The code on my resources.html page was 100% for sure being called but the argument of section=bandwas not being sent to it.

将其更改为 [DPI] 什么也没做...我的 resources.html 页面上的代码肯定是 100% 被调用了,但是section=band的参数没有被发送给它。

Get this... in case you find Apache's documentation impossible to read, Multiviews is the problem. When the browser sees that multiviews is on the server sees /resources/band/ and say "Oh, I'm so smart, I know what that means!" and redirects:

得到这个……如果你发现 Apache 的文档无法阅读,多视图就是问题所在。当浏览器看到多视图在服务器上看到 /resources/band/ 并说“哦,我太聪明了,我知道这意味着什么!” 并重定向:

/resources/band/ -> /resources.html/band/

True story!I changed the +Multiviews to -Multiviews on the virtual host - problem instantly solved.

真实的故事!我将虚拟主机上的 +Multiviews 更改为 -Multiviews - 问题立即解决。

回答by mikepj

I solved this issue by disabling MultiViews in my virtual host Options configuration. I was rewriting something similar to below:

我通过在我的虚拟主机选项配置中禁用 MultiViews 解决了这个问题。我正在重写类似于下面的内容:

Desired rewrite:

期望重写:

/dir/ -> /dir.html

Actual translations:

实际翻译:

/dir/ -> /dir.html (MultiViews)
/dir.html -> /dir.html/ (mod_rewrite: 404, didn't exist)

Disabling MultiViews kept the initial translation from taking place. I could have probably adjusted the rewrite rule to compensate for this, but I wasn't using MultiViews for anything else anyway.

禁用 MultiViews 阻止了初始转换的发生。我本可以调整重写规则来弥补这一点,但无论如何我没有将 MultiViews 用于其他任何事情。

The following post tipped me off on this issue: https://velenux.wordpress.com/2012/07/17/apache-mod_rewrite-multiple-add-path-info-postfix/#comment-1476

以下帖子提示了我这个问题:https: //velenux.wordpress.com/2012/07/17/apache-mod_rewrite-multiple-add-path-info-postfix/#comment-1476