简体   繁体   English

为CloudFlare用户强制使用.htaccess进行HTTPS和非WWW

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

I am using CloudFlare and I want to force HTTPS and Non-WWW by using .htaccess 我正在使用CloudFlare,并且我想通过使用.htaccess强制HTTPS和非WWW

I know there are many examples online already, but for CloudFlare users, normal redirect may cause redirect loops. 我知道已经有很多在线示例,但是对于CloudFlare用户,正常的重定向可能会导致重定向循环。

The closest answer is this one: https://stackoverflow.com/a/34065445/1254581 最接近的答案是这个: https : //stackoverflow.com/a/34065445/1254581

But it only force HTTPS and I need to force non-WWW too. 但这仅强制使用HTTPS,我也需要强制使用非WWW。 Please help to edit this rules: 请帮助编辑此规则:

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`

With Cloudflare, you can use: 借助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