今天说的启用SSL后,让网站的http自动跳转到https上。如果你的主机不是宝塔,那么参照以下教程试试,也许会有惊喜(记得备份,否则惊喜可能变成惊吓),先说Apache强制301跳转到https教程:
Apache自动跳转到 HTTPS
打开网站根目录 .htaccess文件(如果不存在则新建)
Markup
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
301跳转:(推荐)
Markup
<IfModule mod_rewrite.c>RewriteEngine on RewriteBase / RewriteCond %{SERVER_PORT} !^443$ RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]</IfModule>
站点绑定多个域名,只允许www.gworg.com 跳转
Markup
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{HTTP_HOST} ^example.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
###把网址更改为自己的###
高级用法 (可选)
Markup
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
方法一:
Markup
rewrite ^(.*)$ https://$host$1 permanent;
方法二:
适用于 80端口和443 配置在同一个 server{}内
Markup
if ($server_port = 80) { rewrite ^(.*)$ https://$host$1 permanent; }
其他情况, 站点绑定很多域名,只让某个域名跳转:
Markup
set $redirect_https 1; if ($server_port = 80) { set $redirect_https "${redirect_https}2"; } if ($http_host = 'www.gworg.com') { set $redirect_https "${redirect_https}3"; } if ($http_host = 'gworg.com') { set $redirect_https "${redirect_https}3"; } if ($redirect_https = "123") { rewrite ^(.*)$ https://$host$1 permanent; }
配置示例:
Markup
server { listen 80; server_name www.gworg.com; 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.gworg.com; server_tokens off; location / { fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; } }
关于配置有些网站因为开启了伪静态某些规则已经存在,那么删除相同的代码即可,然后保存,查看是否跳转成功,未成功的可以留言给我,一起研究一起学习。Nginx可能比较难,因为不能向Apache那样可以新建文本来实现,nginx只能在配置文件修改,如果有有错误就可能导致网站无法打开或者出现500等错误代码,所以修改配置文件之前一定要先备份,备份,再备份。
专注于网络,好文章定然要分享
声明:本站所有文章或资源,均来自互联网分享。本站不参与制作或存储,内容仅用于互联网爱好者学习和研究,如不慎侵犯了您的权利,请及时联系站长处理删除。敬请谅解!
评论(0)