簡體   English   中英

301 重定向到.html 頁面

[英]301 Redirect for .html pages

在搜索 StackOverflow 並嘗試多種正則表達式方法后,我似乎無法獲得一個重定向,該重定向將使網站上的每個 example.com/example.html 頁面都指向簡單的 example.com/example。 我的 .htaccess 文件如下所示:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

我的第一次嘗試是

RedirectMatch 301 (.*)\.html$ https://example.com$1

但是,雖然它適用於個人.html 頁面,但它導致主頁重定向到https://example.com/index ,即 404。

我最近的嘗試是在底部添加這個

RewriteRule ^(.*)\.html$ /$1 [L,R=301]

那根本行不通。 然后我將同一行放在該行之前

RewriteRule. /index.php [L]

所以塊看起來像這樣

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

但這只會導致網站加載非常緩慢,並且在加載圖像和樣式表時會損壞。

有沒有辦法做到這一點,我只是不明白?

您可以在現有規則之前使用此重定向規則:

RewriteEngine On

# removes .htm, .html, index.htm or index.html - case ignore
RewriteCond %{REQUEST_URI} ^/(.*/)?(?:index|([^/]+))\.html?$ [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

暫無
暫無

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

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