使用rewrite重定向http到https

Apache httpd

RewriteEngine On
# RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301,NE,QSA]

如Server绑定多个域名,需要针对实际请求的主机名重定向,可以把%{SERVER_NAME}替换成%{HTTP_HOST}

Nginx

rewrite ^ https://$host$request_uri permanent;

也可以直接使用return语句

return 301 https://$host$request_uri;