How to redirect all HTTP requests to HTTPS
I'm trying to redirect all insecure HTTP requests on my site (eg http://www.example.com
) to HTTPS ( https://www.example.com
). I'm using PHP btw. Can I do this in .htaccess?
Update: Although this answer has been accepted a few years ago, note that its approach is now recommended against by the Apache documentation. Use a Redirect
instead. See this answer.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Source
The Apache docs recommend against using a rewrite:
To redirect http
URLs to https
, do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
This snippet should go into main server configuration file, not into .htaccess
as asked in the question.
This article might have come up only after the question was asked and answered, but seems to be the current way to go.
我建议使用301重定向:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
链接地址: http://www.djcxy.com/p/5274.html
上一篇: 谷歌分析,从Coldfusion服务器cfhttp击中它
下一篇: 如何将所有HTTP请求重定向到HTTPS