簡體   English   中英

從 .htaccess 配置中刪除結尾的斜杠

[英]Remove ending trailing slash from .htaccess configuration

我創建了一個名為test.example.com的目錄,它在目錄test.example.com中有index.html

我在域上有以下路徑:

http://example.com/test.example.com/

和 .htaccess 具有以下配置



# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.


Options +FollowSymLinks
RewriteEngine On
RewriteBase /


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

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

upon visiting http://example.com/test.example.com/ i would like to redirect on removed trailing slash http://example.com/test.example.com hosted with index.html

好吧,您需要一個外部重定向來刪除潛在的斜杠:

RewriteRule ^/?(test\.example\.com)/$ /$1 [R=301]

如果您還想將該規則應用於該文件夾下的路徑,則可以:

RewriteRule ^/?(test\.example\.com)(.*)/$ /$1$2 [R=301]

最好先從 302 臨時重定向開始,然后再將其更改為 301 永久重定向,前提是您確定一切都已正確設置。 這可以防止在嘗試時出現緩存問題......

此實現同樣適用於 http 服務器主機配置或動態配置文件(“.htaccess”文件)。 顯然重寫模塊需要在 http 服務器內部加載並在 http 主機中啟用。 如果您使用動態配置文件,您需要注意它的解釋在主機配置中完全啟用,並且它位於主機的DOCUMENT_ROOT文件夾中。

和一般性評論:您應該始終更喜歡將此類規則放在 http 服務器主機配置中,而不是使用動態配置文件(“.htaccess”)。 這些動態配置文件增加了復雜性,通常是意外行為的原因,難以調試,並且它們確實減慢了 http 服務器的速度。 它們僅在您無法訪問真正的 http 服務器主機配置(閱讀:非常便宜的服務提供商)或堅持編寫自己的規則的應用程序(這是一個明顯的安全噩夢)的情況下作為最后一個選項提供。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM