簡體   English   中英

如何將domain.com或www.domain.com重定向到https www.domain.com

[英]How to redirect domain.com or www.domain.com to https www.domain.com

我想將我的域名http重定向到https,並且已經使用下面提到的代碼進行了此操作

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https www.domain.com/$1 [L,R=301,NC]

當用戶輸入domain.com時,它將自動重定向到https www.domain.com,但是當用戶僅輸入www.domain.com時,它將重定向到http www.domain.com

請幫我解決這個問題。

您可以使用:

RewriteEngine on
# Test without www
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
# Test for http
RewriteCond %{HTTPS} off 
# Redirect to https www
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]

在您的代碼:

RewriteCond %{HTTP_HOST} ^domain.com [NC]

這是遵循以下規則的條件; 因此,該規則僅適用於所有www請求。

此外,不要使用R=301重定向當你測試新的代碼,除非你確保你的規則是確定的,你可以使用R=302或只R

無論您使用什么代碼,請將以下代碼放在主目錄.htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

如果要強制將每個請求都設置為www ,則應執行以下操作:

RewriteEngine On

RewriteCond %{HTTPS} !=on
#this line above will match any request without `https`

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

#the two lines above will exclude none `www` request and force only `www` request unto `https` and the rule will stop here `L`

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

# the last two line will exclude any `www` request and force only none `www` into `https`

暫無
暫無

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

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