簡體   English   中英

為CloudFlare用戶強制使用.htaccess進行HTTPS和非WWW

[英]Force HTTPS and Non-WWW with .htaccess for CloudFlare Users

我正在使用CloudFlare,並且我想通過使用.htaccess強制HTTPS和非WWW

我知道已經有很多在線示例,但是對於CloudFlare用戶,正常的重定向可能會導致重定向循環。

最接近的答案是這個: https : //stackoverflow.com/a/34065445/1254581

但這僅強制使用HTTPS,我也需要強制使用非WWW。 請幫助編輯此規則:

RewriteEngine On

# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Put the rest of your rewrite rules here`enter code here`

借助Cloudflare,您可以使用:

RewriteEngine On
# www -> https without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# http -> https
# # With Cloudflare
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
# # Without Cloudflare
# RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

暫無
暫無

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

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