Apache 代码: RewriteEngine On # redirect all non-www to https www RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301] # redirect http www to https www RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] Nginx 代码: server { listen [::]:80; listen 80; server_name yourdomain.com www.yourdomain.com; # redirect http to https www return 301 https://www.yourdomain.com$request_uri; } server { listen [::]:443 ssl http2; listen 443 ssl http2; server_name yourdomain.com; # SSL code # redirect https non-www to https www return 301 https://www.yourdomain.com$request_uri; } server { listen [::]:443 ssl http2; listen 443 ssl http2; server_name www.yourdomain.com; # SSL code # other code }