spyc.ai

Free tool

Nginx Redirect Generator

Write nginx redirects as location blocks or a map, with presets for HTTPS, www and trailing slashes.

How it works

What is the Nginx Redirect Generator?

The Nginx Redirect Generator is a free tool that writes nginx redirect configuration from a list of old paths and new URLs. It outputs exact-match location blocks with return 301 or return 302, or a map block for long lists, and adds presets for forcing HTTPS, switching between www and non-www, and normalising trailing slashes.

How to use the Nginx Redirect Generator

  1. Add a row for each redirect: the old path, the new URL and 301 or 302.
  2. Enter your domain and turn on the presets you need.
  3. Choose location blocks for a few redirects or map for many.
  4. Copy the config into the right block, run nginx -t, then reload nginx.

How do I write a 301 redirect in nginx?

The cleanest form is an exact-match location with a return: location = /old-page { return 301 https://example.com/new-page; }. The = makes nginx match that path only, and return ends processing at once. This is what the generator writes in location mode.

nginx's own documentation recommends return over rewrite for simple redirects, because nothing has to be evaluated as a regular expression.

When should I use a map for nginx redirects?

Once you have more than a handful of redirects. A map block is a lookup table: nginx hashes the keys, so a request is checked against thousands of entries in roughly the time it takes to check one. Dozens of separate location blocks work, but they are harder to read and maintain.

The generator writes the map for the http block and a short if with return for the server block. It keys on $uri, the path without the query string. 301 and 302 rows get their own maps.

How do I force HTTPS in nginx?

Give port 80 its own server block that does nothing but return 301 https://$host$request_uri;. The preset writes exactly that, with your domain and its www form in server_name. $request_uri keeps the original path and query string.

The www presets work the same way: a separate server block on port 443 for the hostname you are retiring, returning a 301 to the one you keep. That block needs its own certificate lines, which the output marks with a comment.

Where do nginx redirect rules go?

Location blocks and rewrite lines go inside the server block for the site. A map must sit in the http block, outside any server. Standalone server blocks for HTTPS and www sit beside your main one. Each part of the output carries a comment saying where it belongs.

Always run nginx -t before reloading. A syntax error in one file stops the whole config from loading.

Does nginx read .htaccess files?

No. nginx has no per-directory config files, so rules written for Apache do nothing. If you are moving from Apache, rebuild the redirects here, or convert a whole list at once with the Redirect Map Generator, which outputs both formats.

If the site is still on Apache, use the .htaccess Redirect Generator. After the change, crawl your old URLs with the Broken Link Checker to confirm nothing ends in a 404.

Questions

What is the difference between return and rewrite?
return sends a status and URL straight away. rewrite matches a regular expression and can change the URI internally. For a plain redirect, return is simpler and faster.
Do location blocks match the query string?
No. nginx matches locations against the path only. The generator drops a query string from the old path and warns you.
What do permanent and redirect mean on a rewrite line?
permanent returns a 301 and redirect returns a 302. The trailing slash presets use permanent.
Do I restart or reload nginx after editing?
Reload. nginx -s reload or your service manager's reload command applies the new config without dropping connections.
Is the Nginx Redirect Generator free?
Yes, with a free spyc.ai account. It runs in your browser, so nothing you type or paste is sent to a server.

Technical SEO Tools

More in Technical SEO Tools

Opengraph Viewer

Paste a URL and see the card it produces when someone shares it — title, description, image.

View Opengraph

Open Graph Generator

Write the Open Graph tags, preview the card, copy the markup.

Generate Open Graph tags

Robots.txt Checker

Fetch any site's robots.txt and test one URL against it: allowed, or blocked by which rule.

View robots.txt

Ready when you are

Before your next buyer asks AI about you, ask AI about yourself

One check, four engines, about twenty seconds. We show you the answers before we ask for anything.