Skip to main content

Redirect / Rewrite

Ref: Creating NGINX Rewrite Rules

Redirect HTTP to HTTPS

server {
listen 80;
server_name example.com;

return 301 https://www.example.com$request_uri;
# or
return 301 https://$host$request_uri;
}

See also: HSTS

Redirect all to correct domain name

server {
listen 80 default_server;
listen 443 ssl default_server;
# Use the underscore or empty name to avoid matching a real domain name
server_name "";
return 301 $scheme://www.example.com;
}