【问题标题】:Force trailing slash in .htaccess在 .htaccess 中强制使用斜杠
【发布时间】:2020-06-12 14:12:22
【问题描述】:

这是我当前的 .htaccess 文件:

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /

# Force https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Header set X-UA-Compatible "IE=Edge,chrome=1"

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

如果没有斜杠,我该怎么做才能强制使用斜杠?例如,我当前的 URL 如下所示:

https://amemoirproject.com/chapters/quarter-after

我想强制使用尾部斜杠,使它们看起来像这样:

https://amemoirproject.com/chapters/quarter-after/

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
    

    RewriteCond 确保没有任何具有该名称的文件。如果没有,则添加尾部斜杠。

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2013-09-14
      • 1970-01-01
      • 2017-06-15
      • 2015-04-09
      • 2011-12-08
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多