我想在除文件名之外的所有URL上强制执行尾部斜杠。到目前为止,这是我的.htaccess文件。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
答案 0 :(得分:0)
简单,检查所请求文件不存在的条件,并重定向:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /$0/ [R=301,L,QSA]
请注意,上述规则集将在THE_REQUEST
检查之前。
答案 1 :(得分:0)
让你的htaccess像这样:
RewriteEngine On
# this rule will do 3 things: http->https, www removal, add a trailing slash
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{REQUEST_URI} !/$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [R=301,L,NE,NC]