Check your web redirects and HTTP responses
Redirects with Nginx
Nginx is a widely used web server, especially for VPS hosting and modern architectures. Redirects are configured directly in its configuration files (usually in /etc/nginx/sites-available/), inside server or location blocks.
After each change, reload the configuration: sudo nginx -s reload
301 (permanent) redirect with Nginx
Redirect a specific page:
location = /old-page.html {
return 301 https://www.example.net/new-page.html;
}
Redirect an entire directory:
location /old-directory/ {
return 301 https://www.example.net/new-directory/;
}
Force HTTPS (HTTP → HTTPS):
server {
listen 80;
server_name example.net www.example.net;
return 301 https://$host$request_uri;
}
Force www (non-www → www):
server {
listen 443 ssl;
server_name example.net;
return 301 https://www.example.net$request_uri;
}
Remove www (www → non-www):
server {
listen 443 ssl;
server_name www.example.net;
return 301 https://example.net$request_uri;
}
302 (temporary) redirect with Nginx
Redirect a page:
location = /old-page.html {
return 302 https://www.example.net/new-page.html;
}
Temporarily redirect an entire domain:
server {
server_name example.net;
return 302 https://www.example.net$request_uri;
}
Redirect with rewrite (regular expressions)
For more complex cases, the rewrite directive accepts regular expressions:
Permanent redirect (301):
rewrite ^/old-page/(.*)$ /new-page/$1 permanent;
Temporary redirect (302):
rewrite ^/old-page/(.*)$ /new-page/$1 redirect;
permanent corresponds to a 301 code, redirect corresponds to a 302 code.