Nginx Redirect Generator
Generate nginx redirect rules from a list of URL pairs, using a map block for bulk redirects rather than hundreds of ifs.
Everything you type here is processed on your own device. No input is sent to a server, logged, or stored.
What the Nginx Redirect Generator does
Use this nginx redirect generator to generate nginx redirect rules from a list of URL pairs, using a map block for bulk redirects rather than hundreds of ifs. Paste your text in, pick the options you need, and copy the result straight back out. It is most often used for bulk redirecting URLs after a site migration. We run a second free tools site as well — FreeToolStation.org.
How to use it
- Put one redirect per line as "old path, new URL".
- Pick the status code.
- Choose the map approach for many redirects, or location blocks for a few.
- Run nginx -t to validate before reloading.
How it works
For a handful of redirects, a location block with the = exact-match modifier is the clearest and the fastest — nginx checks exact matches before anything else. For hundreds or thousands, a map block is the right structure: it compiles to a hash lookup that stays fast regardless of size, where a long chain of if blocks is evaluated in order on every request.
The nginx documentation is unusually blunt about if inside a location block, describing it as evil and recommending it be avoided. The map approach uses if only at the server level where its behaviour is predictable, which is the accepted pattern for bulk redirects.
return is preferred over rewrite for a plain redirect. return exits immediately with the status and location you specify; rewrite continues processing and can interact with other directives in ways that are hard to follow. rewrite also cannot emit a 308.
Always run nginx -t before reloading. A configuration syntax error on reload takes the site down, and the error message is not always where the mistake is.
When to use it
- Bulk redirecting URLs after a site migration.
- Adding a handful of redirects to an existing server block.
- Converting an .htaccess redirect list to nginx.
- Setting up redirects that stay fast at scale.
Common mistakes to avoid
- Using hundreds of if or location blocks where a map would be a single hash lookup.
- Reloading without running nginx -t first.
- Using rewrite where return is simpler and more predictable.
Frequently asked questions
What is the best way to do many redirects in nginx?
A map block. It compiles to a hash lookup that stays fast with thousands of entries, unlike a chain of if or location blocks evaluated in order.
Should I use return or rewrite?
return, for a plain redirect. It exits immediately and is predictable. rewrite continues processing and cannot emit a 308.
Why does nginx documentation say if is evil?
Because inside a location block its behaviour is surprising and depends on what else is present. At server level, as used here for a map lookup, it is the accepted pattern.