我试图重定向所有不安全的HTTP请求在我的网站(例如http://www.example.com)到HTTPS (https://www.example.com)。我如何在.htaccess文件中做到这一点?

我用的是PHP。


如果你正在使用Apache, mod_rewrite是最简单的解决方案,网上有很多关于如何做到这一点的文档。例如:http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html

更新:尽管这个答案在几年前就已经被接受了,但请注意,Apache文档现在不建议使用这种方法。改用重定向。请看这个答案。


RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

正如我在这个问题中所说的,我建议您避免盲目地将所有HTTP请求重定向到其HTTPS对等物,因为这可能会使您对安全性产生错误的印象。相反,您应该将HTTP站点的“根”重定向到HTTPS站点的根,并从那里链接到HTTPS。

问题是,如果HTTPS站点上的某些链接或表单使客户端向HTTP站点发送请求,那么在重定向之前,其内容将是可见的。

例如,如果一个通过HTTPS提供服务的页面有一个<form action="http://example.com/doSomething">的表单,并且发送了一些不应该明确发送的数据,浏览器将首先将完整的请求(包括实体,如果它是POST)首先发送到HTTP站点。重定向将立即发送到浏览器,由于大量用户禁用或忽略警告,它很可能被忽略。

Of course, the mistake of providing the links that should be to the HTTPS site but that end up being for the HTTP site may cause problems as soon as you get something listening on the HTTP port on the same IP address as your HTTPS site. However, I think keeping the two sites as a "mirror" only increases the chances of making mistakes, as you may tend to make the assumption that it will auto-correct itself by redirecting the user to HTTPS, whereas it's often too late. (There were similar discussions in this question.)

Apache文档不建议使用重写:

要重定向http url到https,请执行以下操作: < VirtualHost *: 80 > ServerName www.example.com 重定向/ https://www.example.com/ < /虚拟主机> < VirtualHost *: 443 > ServerName www.example.com #……SSL配置在这里 < /虚拟主机>

这段代码应该放在主服务器配置文件中,而不是像问题中要求的那样放在.htaccess中。

这篇文章可能是在问题被提出和回答之后才出现的,但似乎是目前的做法。

在.htaccess文件中添加以下代码:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://[your domain name]%{REQUEST_URI} [R,L]

其中[您的域名]是您网站的域名。

你也可以重定向特定的文件夹从你的域名通过替换上面的最后一行代码:

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" />

PHP方法

<?php
function redirectTohttps() {
    if ($_SERVER['HTTPS']!="on") {
        $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        header("Location:$redirect"); 
    } 
}
?>

.htaccess approch

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

抄袭: www.letuslook.org

这个问题的另一个优势是当负载均衡器开始发挥作用时。

情况如下: -从浏览器到负载均衡器的流量,以及返回,是(应该)HTTPS —负载均衡器与实际WebServer之间的流量为HTTP协议。

因此,PHP或Apache中的所有服务器请求变量都显示连接只是HTTP。服务器上的HTTP和HTTPS目录是相同的。

已批准答案中的重写条件不工作。 它要么是循环,要么就是不起作用。

问题是:如何让它在负载均衡器上工作。

(或者负载均衡器配置错误。这就是我所希望的,因为这样我就可以把问题转移到网络托管公司:-))

按照上面解释的方法进行重定向。只需添加“HTTP严格传输安全”到您的头。这样可以避免中间的人攻击。

编辑apache配置文件(例如/etc/apache2/sites-enabled/website.conf和/etc/apache2/httpd.conf),并将以下内容添加到VirtualHost:

# Optionally load the headers module:
LoadModule headers_module modules/mod_headers.so

<VirtualHost 67.89.123.45:443>
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
</VirtualHost>

https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security

根据GoDaddy.com,这是使用.htaccess将HTTP重定向到HTTPS的正确方法。第一行代码是自解释的。第二行代码检查HTTPS是否关闭,如果关闭,则通过运行第三行代码将HTTP重定向到HTTPS,否则将忽略第三行代码。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

https://www.godaddy.com/help/redirect-http-to-https-automatically-8828

在.htaccess文件中使用以下代码自动将访问者重定向到站点的HTTPS版本:

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果你有一个现有的。htaccess文件:

不要重复“重写引擎”。

确保以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。

Apache 2.4。

<if "%{HTTPS} !~ /on/">
Redirect / https://www.example.com/
</if>

最佳解决方案取决于您的需求。这是对之前发布的答案的总结,并添加了一些上下文。

如果你使用Apache web服务器并且可以更改其配置,请参考Apache文档:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

但你还问是否可以在.htaccess文件中进行。在这种情况下,你可以使用Apache的RewriteEngine:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

如果一切正常,你想让浏览器记住这个重定向,你可以将最后一行更改为:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

但如果你在这个方向上改变了主意,要小心。浏览器会记住它很长一段时间,不会检查它是否改变了。

根据web服务器配置,您可能不需要第一行RewriteEngine On。

如果你在寻找一个PHP解决方案,看看$_SERVER数组和header函数:

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]

此外,参考这更多的细节。如何重定向Http到Https?

除非你需要mod_rewrite做其他事情,使用Apache核心IF指令更干净更快:

<If "%{HTTPS} == 'off'">
Redirect permanent / https://yoursite.com/
</If>

你可以在IF指令中添加更多的条件,比如确保一个没有www前缀的规范域:

<If "req('Host') != 'myonetruesite.com' || %{HTTPS} == 'off'">
Redirect permanent / https://myonetruesite.com/
</If>

使用mod_rewrite做任何事情都有很多熟悉的惯性,但看看这是否适合你。

更多信息:https://httpd.apache.org/docs/2.4/mod/core.html#if

要看到它的行动(尝试没有www。或https://,或用。net代替。com): https://nohodental.com/(我正在做的一个网站)。

如果您无法直接访问站点的apache配置,许多托管平台仍然以这种方式受到限制,那么我实际上会推荐两步方法。为什么Apache自己的文件,你应该首先使用他们的配置选项mod_rewrite的HTTP到HTTPS。

首先,如上所述,你将设置你的.htaccess mod_rewrite规则:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

然后,在你的PHP文件中(你需要在任何适合你的情况下这样做,一些网站将通过一个PHP文件汇集所有请求,其他网站根据他们的需求和正在发出的请求提供各种页面):

<?php if ($_SERVER['HTTPS'] != 'on') { exit(1); } ?>

需要在任何可能在不安全环境中暴露安全数据的代码之前运行上述代码。因此,您的站点通过HTACCESS和mod_rewrite使用自动重定向,而您的脚本确保不通过HTTPS访问时不提供输出。

我想大多数人都不这么想,因此Apache建议在可能的情况下不要使用这种方法。但是,它只需要在开发端进行额外的检查,以确保用户数据的安全。希望这能帮助那些由于我们的托管服务端的限制而不得不考虑使用非推荐方法的人。

我发现了一种方法,强制我的网站的所有页面从http重定向到https的模拟页面,为我工作。

RewriteEngine On 
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

如果你正在使用亚马逊Web服务弹性负载均衡器,它接受https流量并通过http将其路由到你的服务器,这里描述了将所有http流量重定向到https的正确方法:https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb

使用X-Forwarded-Proto报头(包含http或https),它总是包含在来自负载均衡器的http请求中,如下所述:https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html

在httpd.conf文件中:

<VirtualHost *:80>

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

</VirtualHost>

或者在你的根文件。htaccess中:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

好处:它不会尝试重定向本地开发机器上的http流量。

这对我来说很管用:

<IfModule mod_rewrite.c>
 RewriteEngine On
  RewriteCond %{HTTPS} !on
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

例如,http://server/foo?email=someone%40example.com可以正常重定向,没有任何问题。 文件.htaccess位于网站根文件夹(例如名为public_html)。 它是可以使用的 重写%{SERVER_PORT} !^443$ instead of重写%{HTTPS} !on

 Redirect 301 / https://example.com/

(当上面的答案都没用的时候,对我来说是有效的)

奖金:

ServerAlias www.example.com example.com

(固定https://www.example.com未找到)

这将重定向所有的url到https和www

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]

把这段代码放到你的。htaccess文件中 自动将HTTP重定向到HTTPS

RewriteEngine上 重写%{HTTPS} off RewriteRule ^(. *)美元https://% {HTTP_HOST} % {REQUEST_URI} (L, R = 301)

如果希望从tomcat服务器执行以下步骤

在一个独立的Apache Tomcat (8.5.x) HTTP服务器中,如何配置它,以便如果用户输入www.domain.com,他们将被自动转发到https(www.domain.com)站点。

2步方法:在[Tomcat_base]/conf/web.xml的结束标记之前包含以下内容

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>

设置[Tomcat_base]/conf/server.xml连接器设置:

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>

注意:如果您已经进行了https配置并尝试重定向,则只执行第1步。

Not only can you do this in your .htaccess file, you should be doing this period. You will also want to follow the steps here to get your site listed on the HSTS preload list after you implement this redirect so that any requests to the insecure http version of your website never make it past the user agent. Instead, the user agent checks the requested URI against a baked in list of https only websites and, if the requested URI is on that list, changes the protocol from http to https before transmitting the request to the server. Therefore, the insecure request never makes it out into the wild and never hits the server. Eventually when the internet changes over to https only the HSTS preload list will not be needed. Until then, every site should be using it.

为了执行重定向,我们需要启用重写引擎,然后将所有流量从http端口80重定向到https。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourwebsite.tld/$1 [L,R=301]

我尝试了所有我能在互联网上找到的。htaccess配置,但没有一个有效。 然后,我意识到Apache不鼓励使用mod_rewrite。

我的解决方案是在以下文件夹下编辑apache配置文件: /etc/apache2/sites-enabled

您将有一个名为000-default.conf的强制文件和一个名为000-default-le-ssl.conf的ssl配置文件(如果您从letsencrypt/certbot安装了ssl证书)。但是,这些文件的名称可能不同,这取决于您在设置网站时提供的文件名。

在000-default.conf中,我在<VirtualHost *:80>中编辑了以下内容:

<VirtualHost *:80>
    ServerName example.com
    Redirect / https://example.com/
</VirtualHost>

在000-default-le-ssl.conf中,我在<VirtualHost *:80>中编辑了以下内容:

<VirtualHost *:80>
    ServerName example.com
    Redirect / https://example.com/
</VirtualHost>

不需要其他重定向。

保存文件,然后使用sudo service apache2 restart重启apache服务器

以上内容仅适用于Apache服务器。如果在tomcat上运行PHP会怎样?

所以你可以使用PHP代码,无论是Apache/tomcat/Nginx等…

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();
}

经过多次尝试,考虑没有www和有www,这是可行的

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (www\.)?yourdomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我实际上试图让它在没有负载均衡器的EC2实例上工作,因为这需要花钱。我到处都读到。htaccess不是“正确”的方式。很明显,这是可行的,但我只是想按规矩办事。我按照所有示例更新httpd.conf文件,并添加了许多不必要的东西。事实证明,你真正需要的唯一一行是:

Redirect permanent / https://www.yourdomain.com

我的问题是,最初我已经在httpd.conf中的VirtualHost标签中添加了这个,这是很多帖子告诉你要做的,但它不起作用。原来有一个单独的conf文件存储在/etc/httpd/conf中。d调用yourdomain.conf,它已经有了VirtualHost标签,并且覆盖了我的httpd.conf设置。我只是添加了上面的行里面,瞧,它立即重定向到https。端口443不需要单独的VirtualHost。

它现在正在工作,VirtualHost标签看起来是这样的:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html
    ServerAlias www.yourdomain.com
    ErrorLog /var/www/error.log
    CustomLog /var/www/requests.log combined
    Redirect permanent / https://www.yourdomain.com
</VirtualHost>

注意:我已经从certbot(爱那些家伙)的免费证书设置了TLS,只是试图将常规http调用重定向到工作的https站点。