我正在將一個舊的 html 網站移動到 Wordpress。站點結構如下所示:
http://example.com/index.htmlhttp://example.com/about.htmlhttp://example.com/contact.html
在 WordPress 中,它現在看起來像:
https://example.com/https://example.com/about/https://example.com/contact/
這是我的.htaccess檔案
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Redirect 301 /index.html https://example.com/
Redirect 301 /about.html https://example.com/about/
Redirect 301 /contact.html https://example.com/contact/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
(在此處編輯以更正我遇到的錯誤)這有效,除了http://example.com/index.html301 重定向到https://example.com/index.html,然后反復 301 重定向到https://example.com/直到它超時。
該Redirect 301 /index.html https://example.com/被忽略不管我把它放在哪兒,或者重寫規則線之上。
其他有效的重定向從 301 重定向http://example.com/about.html到https://example.com/about.html然后另一個 301 重定向到https://example.com/about/(200 OK)。
我怎樣才能重定向http://example.com/index.html到https://example.com?
uj5u.com熱心網友回復:
您正在使用Redirectwith RewriteRule。這是兩個不同的 Apache 模塊,如果混合使用,它們的作業方式會有所不同。您可以使用以下RewriteRule基于重定向
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^index.html$ https://example.com/ [L,R=301]
RewriteRule ^about.html$ https://example.com/about/ [L,R=301]
RewriteRule ^contact.html$ https://example.com/contact/ [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
確保在測驗這些更新的 htaccess 規則之前清除瀏覽器快取的資料。
uj5u.com熱心網友回復:
http://example.com/index.html301 重定向到,https://example.com/index.html然后重復 301 重定向到,https://onebanquethall.com直到超時。
這不應該發生在 Apache 上。(您在 LiteSpeed 服務器上嗎?)如果您嘗試從index.php(因為您稍后將請求重寫為index.php)重定向,這將是一個問題,但index.html應該沒問題,即使它是一個分配的DirectoryIndex檔案。
您可以通過簡單地將DirectoryIndex腳本頂部的重置為顯式洗掉來解決此問題index.html。例如:
DirectoryIndex index.php
您還可以向該規則添加條件,以便它僅重定向來自客戶端的初始請求,而不是可能發生的任何內部重寫/子請求。
例如,完整的代碼如下所示:
DirectoryIndex index.php
RewriteEngine On
# Specific redirects
RewriteCond %{THE_REQUEST} ^GET\s/index\.html
RewriteRule ^index\.html$ https://example.com/ [L,R=301]
RewriteRule ^about\.html$ https://example.com/about/ [L,R=301]
RewriteRule ^contact\.html$ https://example.com/contact/ [L,R=301]
# Redirect HTTP to HTTPS (everything else)
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# BEGIN WordPress
: etc.
檢查服務器變數的RewriteCond( condition ) 指令THE_REQUEST確保它只會重定向來自客戶端的直接請求。THE_REQUEST包含 HTTP 請求標頭的第一行,如果請求被重寫,則不會更改。該RewriteCond指令僅適用于RewriteRule隨后的第一個指令。
“特定”重定向應在一般 HTTP 到 HTTPS 之前進行,以避免在通過 HTTP 請求舊 URL(這是您正在測驗的內容)時發生雙重重定向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409922.html
標籤:
下一篇:使用htaccess在laravel中將http://重定向到https://時如何洗掉url中的“public”?
