为CloudFlare用户强制使用.htaccess进行HTTPS和非WWW [英] Force HTTPS and Non-WWW with .htaccess for CloudFlare Users

查看:261
本文介绍了为CloudFlare用户强制使用.htaccess进行HTTPS和非WWW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CloudFlare,我想通过使用.htaccess强制HTTPS和非WWW

I am using CloudFlare and I want to force HTTPS and Non-WWW by using .htaccess

我知道网上已经有很多示例,但是对于CloudFlare用户,正常的重定向可能会导致重定向循环。

I know there are many examples online already, but for CloudFlare users, normal redirect may cause redirect loops.

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

The closest answer is this one: https://stackoverflow.com/a/34065445/1254581

但是它仅强制使用HTTPS,我也需要强制使用非WWW。请帮助编辑以下规则:

But it only force HTTPS and I need to force non-WWW too. 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`


推荐答案

使用Cloudflare,您可以使用:

With Cloudflare, you can use:

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]

这篇关于为CloudFlare用户强制使用.htaccess进行HTTPS和非WWW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆