htaccess重定向到https:// www

本文讨论如何使用.htaccess文件正确地将网站重定向到https://www版本,以确保HTTPS安全性和子域名的使用。内容涵盖了各种情况,包括代理、CDN(如CloudFlare)以及避免重定向错误的方法。
摘要由CSDN通过智能技术生成

本文翻译自:htaccess redirect to https://www

I have the following htaccess code: 我有以下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>

I want my site to be redirected to https://www. 我希望将我的网站重定向到https://www. with HTTPS, and enforcing the www. 使用HTTPS并实施www. subdomain, but when I access http://www. 子域,但是当我访问http://www. (without HTTPS), it does not redirect me to https://www with HTTPS. (不使用HTTPS),它不会使用HTTPS将我重定向到https://www


#1楼

参考:https://stackoom.com/question/weHD/htaccess重定向到https-www


#2楼

To first force HTTPS, you must check the correct environment variable %{HTTPS} off , but your rule above then prepends the www. 要首先强制使用HTTPS,必须%{HTTPS} off正确的环境变量%{HTTPS} off ,但是上面的规则会在www.加上前缀www. Since you have a second rule to enforce www. 由于您有第二条规则来实施www. , don't use it in the first rule. ,请勿在第一条规则中使用它。

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]

About proxying 关于代理

When behind some forms of proxying, whereby the client is connecting via HTTPS to a proxy, load balancer, Passenger application, etc., the %{HTTPS} variable may never be on and cause a rewrite loop. 在某些形式的代理之后(客户端通过HTTPS连接到代理,负载均衡器,Passenger应用程序等), %{HTTPS}变量可能永远不会on并导致重写循环。 This is because your application is actually receiving plain HTTP traffic even though the client and the proxy/load balancer are using HTTPS. 这是因为即使客户端和代理/负载平衡器正在使用HTTPS,您的应用程序实际上仍在接收纯HTTP通信。 In these cases, check the X-Forwarded-Proto header instead of the %{HTTPS} variable. 在这些情况下,请检查X-Forwarded-Proto标头,而不要检查%{HTTPS}变量。 This answer shows the appropriate process 该答案显示了适当的过程


#3楼

Michals answer worked for me, albeit with one small modification: Michals的答案对我有用,尽管做了一些小的修改:

Problem: 问题:

when you have a single site security certificate , a browser that tries to access your page without https:// www. 如果您具有单个站点安全证书 ,则该浏览器将尝试在不使用https:// www的情况下访问您的页面。 (or whichever domain your certificate covers) will display an ugly red warning screen before it even gets to receive the redirect to the safe and correct https page. (或您的证书覆盖的任何域) 收到重定向到安全正确的https页面之前 ,都会显示一个难看的红色警告屏幕。

Solution

First use the redirect to the www (or whichever domain is covered by your certificate) and only then do the https redirect. 首先使用重定向到www(或证书所覆盖的任何域),然后再执行https重定向。 This will ensure that your users are not confronted with any error because your browser sees a certificate that doesn't cover the current 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]

#4楼

There are a lot of solutions out there. 有很多解决方案。 Here is a link to the apache wiki which deals with this issue directly. 这是直接处理此问题的apache Wiki的链接。

http://wiki.apache.org/httpd/RewriteHTTPToHTTPS 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

#5楼

If you are using CloudFlare or a similar CDN you will get an infinite loop error with the %{HTTPS} solutions provided here. 如果您使用的是CloudFlare或类似的CDN,则此处提供的%{HTTPS}解决方案将导致无限循环错误。 If you're a CloudFlare user you'll need to use this: 如果您是CloudFlare用户,则需要使用以下命令:

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

#6楼

This will work for both https and www 这将适用于https和www

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值