【问题标题】:Trailing slash automatically added in urlurl 中自动添加斜杠
【发布时间】:2016-11-27 22:41:02
【问题描述】:

当我输入一个网址时,会自动添加一个斜杠。

为什么?

例如,我输入:

example.com/path

它变成:

example.com/path/


我的 .htaccess 文件:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    AddType 'text/css; charset=UTF-8' css

</IfModule>

【问题讨论】:

  • 是的。实际上,执行所有路由的是 index.php 所在的 public/ 目录
  • 如果您的 URL 指向一个目录,则 Apache 出于安全原因会自动添加尾部斜杠。
  • @anubhava 谢谢。更好地为这个问题创建一个正确的答案(而不是评论)并添加一些引用或来源。
  • 好的,已经在下面添加了答案。

标签: .htaccess url web routing


【解决方案1】:

要删除尾部斜杠,您可以使用:

RewriteEngine On

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

【讨论】:

  • 我应该把他的 .htaccess 放在哪里?
【解决方案2】:

如果您的 URL 指向一个目录,则 Apache 的 mod_dir 模块会出于安全原因自动添加尾部斜杠。

Apache mod_dir reference on directory slash

请注意,您可以通过放置此指令来更改此行为:

DirectorySlash Off

但是请记住这个安全警告

安全警告

关闭尾部斜杠重定向可能会导致信息泄露。考虑mod_autoindex 处于活动状态(Options +Indexes)并且DirectoryIndex 设置为有效资源(例如index.html)并且没有为该URL 定义其他特殊处理程序的情况。在这种情况下,带有斜杠的请求将显示index.html 文件。 但不带斜杠的请求会列出目录内容。

【讨论】:

    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多