【问题标题】:Apache's mod_rewrite failing to remove trailing slashApache 的 mod_rewrite 未能删除斜杠
【发布时间】:2017-01-12 09:29:30
【问题描述】:

我有一个 XAMPP Apache 服务器,并添加了一个配置文件来重写带有斜杠的 URL 并重定向到它们的无斜杠对应项。

因此,像 http://example.com/the-audio/ 这样的 URL 会被重定向到 http://example.com/the-audio。问题是,当目录名只有一个单词时,它不起作用。

所以,http://example.com/audio/ 没有删除它的斜线。这对我来说真的很奇怪,检查日志似乎在这种情况下规则不匹配。

这是我的规则(文件中只有一个)

.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)\/+$
RewriteRule ^ %1 [R=301,L]

这是错误转储

error.log

[Wed Jan 11 22:13:32.729812 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] strip per-dir prefix: C:/xampp/htdocs/tecnoedu/audio/ -> audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] applying pattern '^' to uri 'audio/'
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#33046a0/subreq] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/index.php

提前致谢!

【问题讨论】:

标签: php apache .htaccess mod-rewrite trailing-slash


【解决方案1】:

根据您的日志,/audio/ 是一个现有目录。因此,您的规则不会运行,因为第一个条件会阻止它在现有目录上运行。删除第一个条件以解决它。

此外,要在现有目录上执行此操作,您需要关闭 DirectorySlash,否则服务器将自己添加回斜线。注意有some security concern with that

【讨论】:

  • 我会尽快尝试,谢谢!知道如何防止目录列表的安全问题吗?如果没有,我会做我的研究。
  • 是的,关闭目录自动索引并在不使用正斜杠的情况下进行测试,以确保其正常工作。不是我做过的事情,而是阅读了应该涵盖它的安全公告。
  • 我已经介绍过了,现在我有another issue,我把它留在这里,因为我无法联系到你。
  • 没问题,添加了一条希望对您有所帮助的评论。
猜你喜欢
  • 2011-02-20
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2011-06-25
  • 2016-08-07
  • 2017-12-31
相关资源
最近更新 更多