Nginx Rewrite Generator

Generates Nginx rewrite and redirect configuration from a repeatable list of rules: an exact or regex match type, a source path or pattern, a destination path or target, and a flag (301 permanent, 302 redirect, last, or break), producing ready-to-paste location blocks or rewrite directives. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-25

Overview

Introduction

Nginx rewrite and redirect rules have a small but easy-to-mix-up syntax: location blocks versus the rewrite directive, and four different flags that all behave differently.

This tool builds the correct block or directive for each rule you describe, entirely in your browser.

What Is Nginx Rewrite Generator?

A generator for Nginx URL rewrite and redirect rules, covering both `location = /path { return ...; }` blocks for exact matches and `rewrite <regex> <replacement> <flag>;` directives for pattern matches.

It's part of this site's Network Tools collection and produces plain Nginx configuration text with no external dependencies.

How Nginx Rewrite Generator Works

Each rule you add specifies a match type (exact or regex), a source path or pattern, a destination, and a flag. Exact-match rules become a `location = <path> { return 301/302 <to>; }` block (or a `rewrite ^ <to> last/break;` inside that block for the last/break flags), since a literal path match is best expressed as a location block.

Regex-match rules become a single top-level `rewrite <pattern> <replacement> <flag>;` directive, following Nginx's own rewrite directive syntax exactly, including its four flag keywords (permanent, redirect, last, break).

When To Use Nginx Rewrite Generator

Use it when migrating URLs after a site restructure, consolidating old paths into new ones, or forcing a scheme/host redirect, and you want the resulting Nginx syntax to be correct on the first try.

It's also useful as a quick reference for the difference between last and break, which are easy to confuse and behave very differently under nested location blocks.

Features

Advantages

  • Produces syntactically correct Nginx blocks for both exact and regex match types in one tool.
  • Validates regex patterns before generating output, catching a malformed pattern before it reaches your server config.
  • Covers all four of Nginx's rewrite flags (permanent, redirect, last, break) rather than only the two most common ones.

Limitations

  • Doesn't validate that your regex captures ($1, $2, ...) are actually referenced correctly in the destination; that's still on you.
  • Doesn't generate the surrounding `server { }` block, only the location/rewrite snippets to paste inside one.

Examples

A permanent exact-path redirect

Input

matchType: exact, from: /old-page, to: /new-page, flag: permanent

Output

location = /old-page {
    return 301 /new-page;
}

An exact match with the permanent flag becomes a location block returning a 301 status directly, the fastest and most explicit way to redirect one specific path in Nginx.

A regex rewrite with a capture group

Input

matchType: regex, from: ^/blog/(.*)$, to: /articles/$1, flag: last

Output

rewrite ^/blog/(.*)$ /articles/$1 last;

The captured segment after /blog/ is reinserted into the new /articles/ path, and last tells Nginx to re-evaluate location matching against the rewritten URI.

Best Practices & Notes

Best Practices

  • Prefer exact-match location blocks over regex rewrites whenever you're only redirecting one specific literal path; they're faster and easier to read.
  • Always run `nginx -t` after pasting generated rules into your config, before reloading, to catch any typo before it affects live traffic.
  • Use last only when you actually need Nginx to re-search location blocks with the new URI; reach for break when you just want to stop further rewrite processing in the current block.

Developer Notes

Exact-match rules with the last/break flags use `rewrite ^ <to> last/break;` inside the location block rather than `return`, since return only supports fixed status-code redirects, not an internal rewrite that keeps processing the same request; regex-match rules map directly to a single `rewrite` directive using Nginx's own flag keywords with no translation needed.

Nginx Rewrite Generator Use Cases

  • Redirecting a retired URL path to its replacement after a site restructure
  • Building a batch of legacy-to-new-path redirects for a migration
  • Producing a quick-reference example of the last vs break flag distinction for a teammate

Common Mistakes

  • Using last inside a location block that could re-match the same location again, creating an infinite rewrite loop.
  • Forgetting that `return` only takes a fixed destination, while `rewrite` supports regex captures — mixing the two up produces a rule that either doesn't compile or doesn't do what was intended.

Tips

  • Test regex-based rules against a few sample URLs mentally (or with `nginx -T`) before deploying, since a slightly too-greedy pattern can match more paths than intended.

References

Frequently Asked Questions