當使用 htaccess 給出斜杠時,我希望洗掉尾部斜杠。什么是最好的方式來做到這一點,將適用于我現有的規則如下:
# make sure www. is always there
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [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?/$1 [QSA,L]
示例 URL 類似于:
https://www.example.com/this-test/
我當然希望洗掉結尾斜杠。
我試過這個:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )/$ /$1 [R,L]
但這不適用于現有的規則。由于其他規則,它最終重定向到 index.php 頁面。
uj5u.com熱心網友回復:
這樣做:
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?/$0 [QSA,L]
請務必在完全清除瀏覽器快取后對其進行測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358469.html
