我试图重定向所有不安全的HTTP请求在我的网站(例如http://www.example.com)到HTTPS (https://www.example.com)。我如何在.htaccess文件中做到这一点?
我用的是PHP。
我试图重定向所有不安全的HTTP请求在我的网站(例如http://www.example.com)到HTTPS (https://www.example.com)。我如何在.htaccess文件中做到这一点?
我用的是PHP。
当前回答
最佳解决方案取决于您的需求。这是对之前发布的答案的总结,并添加了一些上下文。
如果你使用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']);
}
其他回答
这对我来说很管用:
<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
最佳解决方案取决于您的需求。这是对之前发布的答案的总结,并添加了一些上下文。
如果你使用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']);
}
如果你正在使用亚马逊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流量。
在.htaccess文件中使用以下代码自动将访问者重定向到站点的HTTPS版本:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
如果你有一个现有的。htaccess文件:
不要重复“重写引擎”。
确保以RewriteCond和RewriteRule开头的行立即跟随已经存在的RewriteEngine On。
如果你正在使用Apache, mod_rewrite是最简单的解决方案,网上有很多关于如何做到这一点的文档。例如:http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html