Security 如何将所有HTTP请求重定向到HTTPS
我正在尝试重定向我站点上所有不安全的HTTP请求(例如Security 如何将所有HTTP请求重定向到HTTPS,security,http,.htaccess,redirect,https,Security,Http,.htaccess,Redirect,Https,我正在尝试重定向我站点上所有不安全的HTTP请求(例如http://www.example.com)到HTTPS(https://www.example.com)。如何在.htaccess文件中执行此操作 顺便说一句,我使用的是PHP如果您使用的是Apache,mod_rewrite是最简单的解决方案,并且有很多在线文档说明如何做到这一点。例如:更新:虽然这个答案在几年前就被接受了,但请注意,它的方法是通过Apache文档实现的。改用重定向。看 正如我在中所说,我建议您避免盲目地将所有HTTP
http://www.example.com)到HTTPS(https://www.example.com
)。如何在.htaccess文件中执行此操作
顺便说一句,我使用的是PHP如果您使用的是Apache,mod_rewrite是最简单的解决方案,并且有很多在线文档说明如何做到这一点。例如:更新:虽然这个答案在几年前就被接受了,但请注意,它的方法是通过Apache文档实现的。改用重定向
。看
正如我在中所说,我建议您避免盲目地将所有HTTP请求重定向到它们的HTTPS等价物,因为这可能会给您造成安全性的错误印象。相反,您可能应该将HTTP站点的“根”重定向到HTTPS站点的根,并从那里链接到HTTPS
问题是,如果HTTPS站点上的某个链接或表单使客户端向HTTP站点发送请求,则在重定向之前,其内容将可见
例如,如果通过HTTPS提供服务的其中一个页面的表单上写着
,并且发送了一些不应以明文形式发送的数据,那么浏览器将首先将完整请求(包括实体,如果是帖子)发送到HTTP站点。重定向将立即发送到浏览器,由于大量用户禁用或忽略警告,因此很可能会被忽略
当然,提供本应指向HTTPS站点但最终指向HTTP站点的链接的错误可能会导致问题,只要您在HTTPS站点的相同IP地址上的HTTP端口上获得侦听内容。然而,我认为将这两个站点保持为“镜像”只会增加出错的机会,因为您可能倾向于假设它会通过将用户重定向到HTTPS自动更正自己,而这通常为时已晚。(中也有类似的讨论)建议不要使用重写:
要将http
URL重定向到https
,请执行以下操作:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
服务器名www.example.com
重定向/https://www.example.com/
服务器名www.example.com
# ... SSL配置在这里
此代码段应进入主服务器配置文件,而不是.htaccess
,如问题所述
这篇文章可能是在提问和回答问题之后才出现的,但似乎是当前的做法。将以下代码添加到.htaccess文件中:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://[your domain name]%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
其中[您的域名]是您网站的域名
您还可以通过将上面代码的最后一行替换为以下内容,将特定文件夹从域名中重定向:
RewriteRule ^ https://[your domain name]/[directory name]%{REQUEST_URI} [R,L]
我发现在域名上使用https和www的最佳方式是
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
我喜欢这种从http重定向到https的方法。因为我不需要为每个站点编辑它
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
我建议使用301重定向:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
这是html重定向方法,它可以工作,但不是最好的
<meta http-equiv="Refresh" content="0;URL=https://www.example.com" />
抄袭自:
这个问题的另一个优势是负载平衡器发挥作用
情况如下:
-从浏览器到负载平衡器以及从浏览器到负载平衡器的通信是(应该是)HTTPS
-负载平衡器和实际Web服务器之间的通信是HTTP
因此,PHP或Apache中的所有服务器请求变量都表明连接只是HTTP。服务器上的HTTP和HTTPS目录是相同的
已批准答案中的重写条件不起作用。
它要么给出一个循环,要么就是不起作用
问题是:如何让它在负载平衡器上工作
(或者负载平衡器配置错误。这正是我所希望的,因为这样我可以将问题转移到网络主机公司:-)执行上面解释的重定向操作。只需将“HTTP严格传输安全性”添加到标头中。这将避免中间人攻击。
编辑apache配置文件(例如/etc/apache2/sites enabled/website.conf和/etc/apache2/httpd.conf),并将以下内容添加到VirtualHost中:
#可选加载标题模块:
LoadModule headers\u modules/mod\u headers.so
标头始终设置严格的传输安全性“最大年龄=63072000;包括子域;预加载”
根据GoDaddy.com,这是使用.htaccess将HTTP重定向到HTTPS的正确方法。第一行代码是不言自明的。第二行代码检查HTTPS是否关闭,如果关闭,则通过运行第三行代码将HTTP重定向到HTTPS,否则忽略第三行代码
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
在.htaccess文件中使用以下代码会自动将访问者重定向到站点的HTTPS版本:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
如果您有一个现有的.htaccess文件:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://[your domain name]%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
不要在发动机上重复重写
确保以RewriteCond和RewriteRule开头的行立即跟随已存在的RewriteEngine On。要将所有http
请求重定向到https
,可以使用:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
如果mod rewrite未启用,并且您使用的是apache 2.4,那么您还可以在If
指令中使用Redirect
将http
请求重定向到https
Apache2.4.
<if "%{HTTPS} !~ /on/">
Redirect / https://www.example.com/
</if>
重定向/https://www.example.com/
最佳解决方案取决于您的需求。这是之前发布的答案的摘要,添加了一些上下文
如果您使用Apache web服务器并可以更改其配置,请按照以下步骤操作:
如果一切正常,并且希望浏览器记住此重定向,则可以通过将最后一行更改为:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
但是,如果你可能改变主意,请小心。浏览器会记住它很长时间,并且不会检查它是否改变
您可能不需要f
<VirtualHost *:80>
ServerName www.example.com
Redirect "/" "https://www.example.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
if (!$_SERVER['HTTPS']) {
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
<If "%{HTTPS} == 'off'">
Redirect permanent / https://yoursite.com/
</If>
<If "req('Host') != 'myonetruesite.com' || %{HTTPS} == 'off'">
Redirect permanent / https://myonetruesite.com/
</If>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<?php if ($_SERVER['HTTPS'] != 'on') { exit(1); } ?>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Redirect 301 / https://example.com/
ServerAlias www.example.com example.com
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
step 1:
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOnly</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
step 2:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="443"/>
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="[keystorelocation]" type="RSA" />
</SSLHostConfig>
</Connector>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourwebsite.tld/$1 [L,R=301]
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
if (!((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))){
$redirect = 'https://' . str_replace($_SERVER['SERVER_PORT'], 8443, $_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}