Skip to main content

Nginx Rewrite & Redirect Rule Generator

Generate nginx location blocks for 301/302 redirects, internal rewrites (last/break) with regex capture groups, or reverse-proxy rules — with built-in redirect-loop detection.

Input

One rule per line: source => destination (=> or -> both work). Lines starting with # are ignored. With regex mode on, reference capture groups in the destination as $1, $2, etc.

Matches the location block on a regex (location ~ ...) instead of a literal path.

Forward the original request's query string to the destination.

Output

Nginx Config
 
Validation Warnings
Was this helpful?

More ways to use this tool

REST API

curl -X POST https://api.iotools.cloud/v1/tool/nginx-rewrite-rule-generator \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "rules": "^/blog/(.+)$ => /articles/$1",
    "ruleType": "redirect301",
    "useRegex": "true",
    "preserveQueryString": "true"
  }'

Swap in your own key from your account. The tool's fields are the body — no wrapper.

Ask an AI agent

Use the IOTools `nginx-rewrite-rule-generator` tool (Nginx Rewrite & Redirect Rule Generator) on this input:

YOUR_INPUT_HERE

Paste this at any agent connected to the IOTools MCP server, then add your input.

Embed widget

<iframe
  src="https://iotools.cloud/embed/nginx-rewrite-rule-generator/"
  width="100%" height="520" frameborder="0" scrolling="no" loading="lazy"
  title="Nginx Rewrite & Redirect Rule Generator — iotools.cloud"
  sandbox="allow-scripts allow-forms allow-same-origin allow-downloads allow-popups allow-popups-to-escape-sandbox"
  allow="clipboard-write"
  style="width:100%;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden"></iframe>
<script src="https://iotools.cloud/embed.js" async></script>

Drop this into your own page — free, no key required, just a link back.

Cost per API/MCP callFrom 5 credits
Need more credits?View pricing

Also available with

Guides

The Nginx Rewrite & Redirect Rule Generator builds a ready-to-paste location block for a list of nginx rules — permanent/temporary redirects (return), internal URL rewrites with regex capture groups (rewrite ... last/break), or reverse-proxy routes (proxy_pass). It also flags common redirect-loop mistakes before you deploy them.

How to use this tool

  1. Enter one rule per line as source => destination (-> also works). Lines starting with # are skipped.
  2. Pick a Rule Type — a 301/302 redirect, an internal rewrite (last restarts location matching on the new URI, break stops processing without a new search), or a reverse proxy.
  3. Turn on Treat source as a regex pattern to match on a pattern instead of a literal path — reference capture groups in the destination as $1, $2, etc. (e.g. ^/blog/(.+)$ => /articles/$1).
  4. Toggle Preserve query string to control whether the original request's query string is forwarded to the destination.
  5. Copy or download the generated config, and check the Validation Warnings panel — it flags a rule whose source and destination are identical, or two rules that redirect to each other, both of which cause an infinite redirect loop in a browser.

Return vs. rewrite vs. proxy_pass

  • Redirect (return) sends the browser a real HTTP 301/302 response — the fastest and most predictable way to do a plain URL redirect, and the recommended approach over rewrite ... permanent for simple cases.
  • Rewrite (last/break) changes the URI internally, with no redirect visible to the browser — useful for routing pretty URLs to a different internal path.
  • Reverse Proxy (proxy_pass) forwards the request to another server or backend rather than serving it locally.

Frequently asked questions

Why did it flag a loop but the redirect still seems fine? Loop detection only catches the two most common mistakes: a rule whose source and destination are the same path, and two rules that redirect to each other. A longer chain (A → B → C → A) or a loop that only appears after nginx's regex/rewrite processing isn't checked — always test a generated rule against a real request before deploying it.

I need to build a whole server block, not just one rule. Use the Nginx Server Block Generator instead — it assembles a complete server { } block (listen port, SSL, gzip, proxy mode) rather than a single location rule.

I just need simple exact-path redirects for multiple platforms, not nginx-specific syntax. The Redirect Rules Generator generates the same 301/302 redirect from a plain URL list for nginx, Apache, and Next.js at once — pick that one if you don't need regex or reverse-proxy rules.

Is my input sent anywhere? No. Every rule is generated entirely in your browser — nothing you type leaves your device.

location blockproxy_passreverse proxyvhosturl mappingregex capture groupweb server configredirect loop

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.