启用SSL强制跳转到HTTPS

Apache自动跳转到 HTTPS

网站根目录下修改 .htaccess

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

站点绑定多个域名,只允许www.mlsha.cn 跳转

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^mlsha.cn [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mlsha.cn [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

###把网址更改为自己的###

高级用法 (可选)

RewriteEngine on
# 强制HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_secure [OR]
RewriteCond %{REQUEST_URI} ^something_else_secure
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# 强制HTTP
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_public [OR]
RewriteCond %{REQUEST_URI} ^something_else_public
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Nginx 自动跳转到HTTPS

方法一: 

rewrite ^(.*)$ https://$host$1 permanent;

方法二:
适用于 80端口和443 配置在同一个 server{}内

if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}

其他情况, 站点绑定很多域名,只让某个域名跳转:

set $redirect_https 1;
if ($server_port = 80) {
set $redirect_https "${redirect_https}2";
}
if ($http_host = 'www.mlsha.cn') {
set $redirect_https "${redirect_https}3";
}
if ($http_host = 'mlsha.cn') {
set $redirect_https "${redirect_https}3";
}
if ($redirect_https = "123") {
rewrite ^(.*)$ https://$host$1 permanent;
}

配置示例:

server {
listen       80;
server_name  www.mlsha.cn;
rewrite ^ https://$http_host$request_uri? permanent;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/cacert.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
server_name www.mlsha.cn;  
server_tokens off;
location / {
fastcgi_param   HTTPS               on;
fastcgi_param   HTTP_SCHEME         https;
}
}

暂无评论

发表评论

相关推荐

神龙出海副图

MMS:=MA(SMA((L+H+2*C)/4-REF((L+H+2*C)/4,1),5,1)/SMA(ABS((L+H+2*C)/4-REF((L+H+2*C)/4,1)),5,1)*100,1)+50; G1:=IF(-MMS>95,0,20);UOO:=(-(((CLOSE-LLV(CLOSE,7))/(HHV(CLOSE,7)-LLV(CLOSE,7))*100=100)&g ...

半年总结

2020年上半年是一个比较特殊的半年时间,说它特殊主要有兩點原因,第一點就是因为疫情影響,整個2月月一大半的時間都是在家辦公的,第二點就是上線了精靈盛典這款遊戲,拉高了整體的全年業績完成。 在過去的半年 ...