簡體   English   中英

Avoid multiple page redirects http://example.com > https://example.com -> https://www.example.com

[英]Avoid multiple page redirects http://example.com > https://example.com -> https://www.example.com

如果用戶鍵入 domain.com,GTMetrix 和 Google Pagespeed Insights 報告說,我的網站由於多個頁面重定向而變慢:

URL:                       TIME SPENT:
http://example.com/        285ms
https://example.com/       452ms
https://www.example.com    0ms

我試圖只獲得一個(有條件的?)重定向,它總是會出現在 https 和 www 子域中。

這是當前的 htaccess 代碼:

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

它有效,但是有沒有人痴迷於速度,可以幫助它更快地進入一個重寫條件?

我在堆棧溢出(和其他地方)進行了無休止的搜索,但沒有其他選擇可以將一條語句中的所有內容與條件結合起來,並使其比目前的解決方案更快。

我真的很感激你的見解!

將 R 標志更改為 301:

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]

因為默認情況下,它是一個 302(臨時)重定向,您可以避免很多重定向。 http://httpd.apache.org/docs/current/rewrite/flags.html

暫無
暫無

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

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