nginx Redirect Generator (Force HTTPS, www Canonical, 301/302)
Build nginx config to paste into your server blocks by choosing the rules you need: force HTTPS, canonical www or non-www host, trailing slashes, and per-URL 301/302 redirects. Characters that would break the config, such as ; or line breaks, are rejected, and redirect loops and duplicates are flagged. On Apache, use the .htaccess Generator instead; for what each code means, see the HTTP Status Codes reference.
🔒 Runs in your browser · Your data is never sent to a server
Read this before you deploy
Do not apply the output to production as is. Run nginx -t to check the syntax, then reload. Back up your config first and replace placeholder values such as ssl_certificate with your own.
HTTPS
Choose this if a proxy terminates HTTPS. Instead of a separate server block, the output checks the X-Forwarded-Proto header with an if.
www or non-www
Path normalization
Per-URL redirects
The old path starts with /. The destination is a path starting with / or a URL starting with https://. The status can be 301 (permanent), 302 (temporary), 307 or 308, and defaults to 301. Lines starting with # are ignored. Write non-ASCII characters in the old path as they are (percent-encoding is not allowed).
Checks
Checklist after deploying
- Before the change, back up your current nginx config files (for example under /etc/nginx/).
- Paste the output where its comments say. Blocks that start with "server { }" go outside existing server blocks, inside the http context.
- Run nginx -t to check the syntax, then apply it with nginx -s reload (or systemctl reload nginx).
- Run the command below to check that the Location and status (such as 301) are what you expect and that the redirects do not repeat many times.
curl -sIL http://example.com/ | grep -iE "^(HTTP|Location)"How to use
- Choose the rules you want, such as "Force HTTPS", "Unify www" and "Trailing slash".
- If you unify www, enter the bare domain, such as example.com, in the "Domain" field.
- To forward individual URLs, enter one "old path destination" pair per line in "Per-URL redirects".
- Review the checks, then use "Copy" or "Download" and paste the result into your config where the comments say.
Notes
- The output is a set of snippets. Blocks that start with server { } are added as separate server blocks. Everything under the comment that says to add it inside your main server block goes inside the existing server block that handles your domain.
- The force-HTTPS server block uses listen 80. If you already have a server block with the same listen and server_name, merge them so they do not conflict.
- In nginx, if is only safe in a few situations. The if blocks this tool writes contain just a return or rewrite, which is the safe pattern. If you add more to them by hand, read the If Is Evil page in the nginx documentation first.
- A location is matched against the path after percent-decoding. That is why % is not allowed in the old path and non-ASCII characters are written as they are.
- The rule that adds a trailing slash applies to every URL without a dot, so check that API paths are not redirected by accident. The rule that removes it works inside a server block that has a root, and skips real directories.
- Per-URL redirects are written as locations. When exact matches (location =) and prefix matches (regex) are mixed, the order in the file decides which one wins.
- Browsers and search engines cache a 301 as a permanent move, which is hard to undo. Try a 302 first until you are sure it works.
- Hand-written regular expressions are not supported. For complex conditions, edit the generated output by hand.
Glossary
server block
The nginx directive that groups the settings for one site (a domain and port combination). server_name decides which domains it handles.
301 / 302 redirect
301 means a permanent move and 302 a temporary one. Search engines treat a 301 as passing ranking signals to the new URL. 307 and 308 forward while keeping the request method (such as POST).
$request_uri
An nginx variable holding the original requested path and query string, before any rewriting. Used to carry the path and query over in a redirect.
location
The nginx directive that routes requests by URL path. = means an exact match and ~ a regular expression match.
FAQ
Where do I paste the generated config?
Follow the comments in the output. Blocks that start with server { } are added as separate server blocks (usually in a file under /etc/nginx/conf.d/, inside the http context). The part under the comment about your main server block goes inside the existing server block that handles that domain. Run nginx -t before you reload.
After enabling Force HTTPS my browser says there were too many redirects.
When a CDN or reverse proxy such as Cloudflare terminates HTTPS, nginx receives plain HTTP, so redirecting everything on port 80 to HTTPS loops forever. Choose the option for a site behind a CDN or reverse proxy: the output then redirects only when the X-Forwarded-Proto header says http. If your CDN has its own "Always use HTTPS" option, you can use that instead.
Why does the tool use separate server blocks instead of if and rewrite?
In nginx, if only works safely in a few places and can behave in surprising ways. For host and scheme redirects, a dedicated server block with return 301 is the recommended pattern: it is fast and hard to get wrong. This tool uses if only where that is safe, such as the proxy check and the index.html rule, and only with a return inside.
Why can I not use % or ? in the old path?
An nginx location is matched against the path after percent-decoding, without the query string. A pattern like /a%20b therefore never matches, and non-ASCII characters have to be written as they are. ? and # are not part of the path. Characters such as ; { } $, quotes and line breaks are rejected because they could break the config syntax or inject extra directives.
What is the difference between exact and prefix matching?
Exact matching uses location = and forwards only the URL /old. Prefix matching uses a regular-expression location and also forwards everything under it, so /old/page goes to /new/page. Use prefix matching when you move a whole directory and exact matching for a single page. A destination inside the old path would loop forever with prefix matching, so the tool reports it as an error.
Share this page
