安裝 SSL 證書後,你需要將所有 HTTP 流量重新導向到 HTTPS。沒有重新導向,訪問 http://yourdomain.com 的訪客不會使用加密連線——即使 HTTPS 可用。
使用 301(永久)重新導向以便搜尋引擎將所有排名訊號轉移到 HTTPS URL。
Nginx
新增一個單獨的 server 塊用於埠 80 重新導向所有請求:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
這會保留完整的 URL 路徑:http://example.com/page?q=1 → https://example.com/page?q=1。
編輯後,測試並重新載入:
sudo nginx -t && sudo systemctl reload nginx
Apache
方案一:VirtualHost 重新導向(推薦)
新增到你的 Apache 配置:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
方案二:.htaccess(共享主機)
如果你沒有 VirtualHost 配置的訪問許可權(共享主機),新增到網站的 .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
需要啟用 mod_rewrite。
更改後:
sudo apachectl configtest && sudo systemctl reload apache2
驗證重新導向
# Should return 301 with Location: https://...
curl -I http://yourdomain.com
預期輸出:
HTTP/1.1 301 Moved Permanently
Location: https://yourdomain.com/
HSTS:雙重鎖定
確認重新導向正常後,新增 HSTS(HTTP Strict Transport Security)。它告訴瀏覽器始終使用 HTTPS,即使使用者輸入 http://:
Nginx:
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
Apache:
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
先用短的 max-age(如 300 秒)測試,確認一切正常後再增加到 2 年(63072000)。
警告: 一旦 HSTS 以長
max-age啟用,即使你移除 HTTPS,瀏覽器也會拒絕透過 HTTP 連線。確保你的 HTTPS 配置穩定後再設定長期限。
常見重新導向模式
www 重新導向到非 www + HTTPS
# Nginx: www → non-www, HTTP + HTTPS → HTTPS
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
return 301 https://example.com$request_uri;
}
非 www 重新導向到 www + HTTPS
server {
listen 80;
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
return 301 https://www.example.com$request_uri;
}
重新導向整個舊域名
server {
listen 80;
listen 443 ssl;
server_name olddomain.com www.olddomain.com;
ssl_certificate /etc/ssl/old-fullchain.pem;
ssl_certificate_key /etc/ssl/old-privkey.pem;
return 301 https://newdomain.com$request_uri;
}
舊域名也需要有效的 SSL 證書——瀏覽器必須先建立 HTTPS 才能接收重新導向。使用覆蓋兩個域名的 SAN 證書,或為舊域名單獨準備一個證書。
故障排查
重新導向迴圈 (ERR_TOO_MANY_REDIRECTS)
這通常意味著你的 HTTPS server 塊也在重新導向到 HTTPS。檢查只有埠 80 的塊有重新導向——埠 443 的塊應該正常提供內容。
另一個原因:負載均衡器或代理(Cloudflare、AWS ALB)終結 SSL 並轉發 HTTP 到你的伺服器。你的伺服器看到 HTTP 就重新導向。透過檢查 X-Forwarded-Proto 頭修復:
# Behind a proxy/load balancer
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
搜尋引擎中快取的舊 HTTP URL
設定重新導向後,告訴 Google 變化:
- 將
<link rel="canonical">更新為使用https:// - 將站點地圖 URL 更新為
https:// - 在 Google Search Console 中新增 HTTPS 資源
Google 會在跟隨 301 重新導向時逐步更新已索引的 URL。
常見問題
應該同時將 www 重新導向到非 www(或反之)嗎?
是的。選擇一種規範形式並重新導向另一種。這避免搜尋引擎中的重複內容:
# Redirect www to non-www (Nginx)
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
重新導向會影響 SEO 嗎?
301 重新導向將排名訊號傳遞給目標 URL。Google 推薦使用 301 重新導向進行 HTTP 到 HTTPS 遷移。可能有小幅臨時波動,但長期 SEO 會因 HTTPS 排名訊號而改善。
重新導向後的混合內容怎麼辦?
重新導向處理頁面 URL,但如果你的 HTML 以 http:// URL 引用資源(圖片、指令碼、CSS),瀏覽器會阻止它們或顯示警告。參見我們的混合內容修復指南。
如何測試重新導向是否正常工作?
# Check redirect chain
curl -ILs http://yourdomain.com | grep -E '^HTTP|^Location'
預期輸出:
HTTP/1.1 301 Moved Permanently
Location: https://yourdomain.com/
HTTP/2 200
第一個響應應該是 301 帶有 HTTPS Location,最終響應應該是 200。
應該在 DNS 級別還是伺服器級別重新導向?
伺服器級別(Nginx/Apache 配置或 .htaccess)。DNS 級別重新導向(如 Cloudflare 的 Page Rules)可以工作但增加了網路跳數,且你對重新導向行為的控制更少。伺服器級別重新導向更快更可靠。