簡體   English   中英

htaccess 重定向到 https://www

[英]htaccess redirect to https://www

我有以下 htaccess 代碼:

<IfModule mod_rewrite.c>

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

</IfModule>

我希望我的網站被重定向到https://www. 使用 HTTPS,並強制使用www. 子域,但是當我訪問http://www. (沒有 HTTPS),它不會使用 HTTPS 將我重定向到https://www

要首先強制使用 HTTPS,您必須檢查正確的環境變量%{HTTPS} off ,但是您上面的規則然后在www. 由於您有第二條規則來強制執行www. ,不要在第一條規則中使用它。

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

關於代理

在某些形式的代理之后,即客戶端通過 HTTPS 連接到代理、負載均衡器、Passenger 應用程序等, %{HTTPS}變量可能永遠不會on並導致重寫循環。 這是因為即使客戶端和代理/負載平衡器使用 HTTPS,您的應用程序實際上也接收純 HTTP 流量。 在這些情況下,檢查X-Forwarded-Proto標頭而不是%{HTTPS}變量。 這個答案顯示了適當的過程

Michals 的回答對我有用,盡管有一個小的修改:

問題:

當您擁有單個站點安全證書時,瀏覽器會嘗試在沒有 https://www 的情況下訪問您的頁面。 (或您的證書涵蓋的任何域)甚至會在收到重定向到安全且正確的 https 頁面之前顯示一個丑陋的紅色警告屏幕。

解決方案

首先使用重定向到 www(或您的證書涵蓋的任何域),然后才進行 https 重定向。 這將確保您的用戶不會遇到任何錯誤,因為您的瀏覽器會看到未涵蓋當前 url 的證書。

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果您使用 CloudFlare 或類似的 CDN,您將在此處提供的 %{HTTPS} 解決方案中遇到無限循環錯誤。 如果您是 CloudFlare 用戶,則需要使用以下命令:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

糟糕的解決方案以及為什么!

永遠不要使用下面的解決方案,因為當您使用他們的代碼時:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

瀏覽器轉到:

http://example.com

然后重定向到:

https://example.com

然后重定向到:

https://www.example.com

這對服務器的請求太多了。

大多數甚至接受的答案都有這個問題。


最佳解決方案和答案

此代碼具有[OR]條件以防止在 url 上進行雙重更改!

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

這是我為代理而不是代理用戶找到的最佳方式

RewriteEngine On

### START WWW & HTTPS

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

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### END WWW & HTTPS

有很多解決方案。 這是直接處理此問題的 apache wiki 的鏈接。

http://wiki.apache.org/httpd/RewriteHTTPToHTTPS

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

要將http://https://重定向到https://www您可以在所有版本的 apache 上使用以下規則:

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

阿帕奇 2.4

RewriteEngine on

RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

請注意,自 apache 2.4起, %{REQUEST_SCHEME} 變量可供使用。

如果您使用 CloudFlare,請確保使用類似的東西。

# BEGIN SSL Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END SSL Redirect

這將使您免於重定向循環,並將您的站點安全地重定向到 SSL。

PS 檢查 mod_rewrite.c 是個好主意!

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

注意:確保您已完成以下步驟

  1. 須藤 a2enmod 重寫
  2. 須藤服務 apache2 重啟
  3. 在位於 /etc/apache2/sites-available/000-default.conf 的 vhost 文件中添加以下內容
<Directory /var/www/html>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
  Require all granted
</Directory>

現在您的 .htaccess 將起作用,您的站點將重定向到 http:// 到https://www

類似於 Amir Forsati 的解決方案htaccess 重定向到 https://www但對於可變域名,我建議:

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

在您的 .htaccess 文件中設置

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我使用了這個網站上的以下代碼,效果很好https://www.freecodecamp.org/news/how-to-redirect-http-to-https-using-htaccess/

 RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

希望能幫助到你

我嘗試第一個答案,但它不起作用......這項工作:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# 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

這將適用於https和www

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

暫無
暫無

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

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