![](/img/trans.png)
[英]RewriteRule htaccess to always remove trailing slash even directory
[英].htaccess Remove trailing / unless directory
到目前為止,這是我的 .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
## REMOVE TRAILING /
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
我有這樣的結構:
https://example.com/page-name
它作為字符串查詢返回到index.php
,在這種情況下它將是$_SERVER['REQUEST_URI'] = 'page-name'
出於 SEO 和規范原因,我想使用 301 重定向來重定向尾隨 /,如下所示:
如果他們輸入:
https://example.com/page-name/
應該重定向到
https://example.com/page-name
我當前的 .htaccess 重定向到https://example.com/index.php
如果他們輸入尾隨 / 並且我不知道如何修復它。
你必須重新排序你的規則,在內部規則之前保留你的外部重寫規則(帶有 R 標志的規則),
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [L,R]
#other rules
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.