nginx redirect generator
Enter your redirects, choose the options and copy the generated configuration.
After deploying, test your redirects with the Redirect checker or the Bulk checker.
Want to understand the rules? Read the nginx redirect guide.
How the nginx redirect generator works
Enter your domain, decide whether HTTP should be redirected to HTTPS, pick a preferred host (don't change, without www or with www) and add single page redirects as old-path new-path-or-URL, one per line. The status code you choose (301, 302, 307 or 308) is used for the page redirects. The generator writes complete server blocks you can adapt to your configuration.
For example.com with HTTPS, without www and one page redirect, the output is:
# Generated with https://redirect-tester.com/nginx-redirect-generator/
# HTTP to HTTPS and example.com in one redirect
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
# www.example.com to example.com
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
# ssl_certificate and ssl_certificate_key must cover this host too
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
# ... your existing configuration ...
# Page redirects (301)
location = /old-page {
return 301 /new-page;
}
}
What the server blocks do
- Port 80: every HTTP request, with or without www, is sent directly to
https://example.com.$request_urikeeps the path and query string. - Port 443 for the non-preferred host:
https://www.example.com/is redirected to the preferred host. Together, both blocks make sure every variant reaches the final URL in one hop, without a redirect chain. location = /old-pagematches exactly this path and belongs in your main server block.returndoes not append the query string to a target like/new-page. If you need it, usereturn 301 /new-page$is_args$args;.
return is faster and easier to read than rewrite, and nginx recommends it for simple redirects. The difference between permanent and temporary codes is explained in 301 vs. 302.
Certificates for both hosts
Before nginx can send a redirect on port 443, the TLS handshake has to succeed. The certificate must therefore cover both example.com and www.example.com, otherwise visitors of the non-preferred host see a certificate warning instead of the redirect. With Let's Encrypt, request both names in one certificate, e.g. certbot --nginx -d example.com -d www.example.com, and add the ssl_certificate lines to both 443 blocks.
Test, reload, check
Add the blocks to your site configuration, usually under /etc/nginx/sites-available/ or /etc/nginx/conf.d/, and merge the last block with your existing server block. Then run:
sudo nginx -t
sudo systemctl reload nginx
nginx -t checks the syntax. Only reload if it reports success, because a broken configuration will not be loaded. Afterwards, test the HTTP, www and old page URLs with the redirect checker. Background and more examples are in the nginx redirect guide and the nginx documentation on return.
Frequently asked questions
- Why are there separate server blocks for the redirects?
Separate server blocks with only a
returnare the most efficient way to redirect whole hosts in nginx. nginx picks the block by port andserver_nameand answers immediately, withoutifconditions. The nginx documentation explicitly advises against usingif ($host ...)for this.- I still see the old behaviour after the change. Why?
Either nginx was not reloaded, the configuration file is not included, or your browser serves a cached
301. Check withnginx -Twhether your blocks are part of the active configuration, and test with the redirect checker, which does not use a browser cache.- Can I use 308 instead of 301 for the host redirect?
Yes. Replace
return 301withreturn 308. Both are permanent and a strong canonicalization signal for Google. The only difference is that308requires clients to keep the request method, which matters for POST requests. See 307 vs. 308.
More free tools
-
Bulk checker
Check up to 20 URLs at once, e.g. your redirect map after a domain migration. Export as CSV.
-
Header checker
See all HTTP response headers of every hop, including Location, Cache-Control and HSTS.
-
.htaccess redirect generator
Generate Apache .htaccess rules for single pages, HTTPS and www redirects.
-
HTTP status codes
All HTTP status codes explained, with notes on how Google treats them.
-
JSON API
Check redirects from your scripts and CI pipelines with a simple JSON API.