簡體   English   中英

http://example.com、http://www.example.com和https://example.com到https://www.example.com

[英]http://example.com, http://www.example.com and https://example.com to https://www.example.com

關於堆棧溢出的第二個問題! 我們的目標是將對我們網站的所有請求永久重定向到https://www.example.com 目前,我們在下面的代碼中將http://example.comhttp://www.example.com重定向到https://www.example.com ,但是,在花了很多時間在Stack Overflow上尋找之后解決方案中,我們還沒有找到將https://example.com重定向到https://www.example.com 非常感謝您的幫助。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

最好用一個規則來處理:

RewriteEngine On

# add www and force https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
  • 將此規則作為您的第一條規則。
  • 在測試之前,請確保清除瀏覽器緩存。
  • 測試完成后,將302更改為301

為了補充@anubhava的Apache優雅解決方案,我們最近找到了一種在Nginx上執行相同操作的方法:

server {
        listen 80;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 80;
        server_name www.example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 443;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}

暫無
暫無

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

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