使用现有规则的 htaccess 删除尾部斜线

Remove trailing slash with htaccess with existing rules

我希望删除使用 htaccess 给出的尾部斜杠。符合我现有规则的最佳方法是什么,如下所示:

  # make sure www. is always there
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  # if requested url does not exist pass it as path info to index.php
  RewriteRule ^$ index.php?/ [QSA,L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) index.php?/ [QSA,L]

示例 URL 类似于:

https://www.example.com/this-test/

我当然希望删除结尾的斜杠。

我试过这个:

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

但这不适用于现有的规则。由于其他规则,它最终重定向到 index.php 页面。

这样说:

Options -MultiViews
DirectoryIndex index.php
RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

# if requested url does not exist pass it as path info to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/[=10=] [QSA,L]

确保在完全清除浏览器缓存后进行测试。