【问题标题】:How to stop RewriteRule from removing trailing slash如何阻止 RewriteRule 删除尾部斜杠
【发布时间】:2014-04-08 18:28:13
【问题描述】:

我的“fs”文件夹中的 .htaccess 文件中有以下内容:

RewriteEngine on
RewriteRule ^(.*)$ index.php [L]

基本上我希望将所有 url 发送到 index.php 文件,但我需要在我的 url 中保留尾部斜杠。 RewriteRule 似乎总是从 url 中删除尾部斜杠,我需要保留它们。

http://example.com/fs/path/ 被重定向到 http://example.com/fs/path

如何防止尾部斜杠被删除?我需要保留斜杠。有什么想法可以做到这一点吗?

编辑 这是父 .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^auth(.*)$ auth.php?$1 [L]

RewriteRule ^file/(.*)$ file.php?path=/$1 [L]

RewriteRule ^(.*).css$ css.php?path=/$1 [NC]

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

【问题讨论】:

  • 此规则不会(不能)删除尾部斜杠。你还有其他规定吗?
  • 这是我在这个文件夹中的整个 .htaccess 文件。我在父文件夹中有另一个 .htaccess 文件。这会导致问题吗?如果是,有没有办法忽略父文件夹中的那个?
  • 你能显示父 .htaccess 代码吗?
  • 刚刚将父 .htaccess 添加到原始帖子
  • 好吧好吧。 /fs/path 是一个真实的目录吗?它也有 .htaccess 吗?

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

让你的/fs/.htaccess 像这样:

RewriteEngine on
RewriteBase /fs/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

【讨论】:

  • 这仍然会更改 url 并删除尾部斜杠。会有某种 apache conf 设置这样做吗?
  • 那里很可能有一些规则。在 firebug 中运行这个请求,看看你在 net 选项卡中得到了什么。
  • GET /fs/Projects/ 301 永久移动 GET Projects 200 OK
  • 糟糕。我发现我正在使用 Laravel 安装,它在找到尾部斜杠时进行重定向。所以 .htaccess 文件没有问题。感谢您的帮助!
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 2015-01-15
相关资源
最近更新 更多