设置HTTP请求自动跳转HTTPS

Nginx

在需要跳转的HTTP站点下添加以下rewrite语句,实现HTTP访问自动跳转到HTTPS页面。

1
2
3
4
5
6
7
8
server {
listen 80;
server_name www.certificatestests.com; #将www.certificatestests.com修改为您证书绑定的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent; #将所有HTTP请求通过rewrite重定向到HTTPS。
location / {
index index.html index.htm;
}
}

Apache

在httpd.conf文件中的 中间,添加以下重定向代码。

1
2
3
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

.htaccess

在Web目录下打开.htaccess文件(如没有,需新建该文件),添加以下rewrite语句,实现HTTP访问自动跳转到HTTPS页面。

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$ [NC] # 将yourdomain.com修改为您证书绑定的域名,例如:example.com。
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L] # 将yourdomain.com修改为您证书绑定的域名,例如:example.com。
坚持原创技术分享,您的支持将鼓励我继续创作!
0%