Nginx Redirect Rule Generator
Build production-ready Nginx redirect rules in seconds. Pick a redirect type, fill in the inputs, and copy a clean server or location block.
Redirect Type
Redirect Options
Generated Nginx Configuration
Paste into your nginx.conf or a site config file under /etc/nginx/sites-available/.
# HTTP → HTTPS — 301 permanent
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://$host$request_uri;
}After editing, validate with sudo nginx -t and reload with sudo systemctl reload nginx.
Always test the redirect with curl -I before pointing real traffic at it.
About Nginx Redirect Rule Generator
The Nginx Redirect Rule Generator is a free online tool that builds copy-ready Nginx redirect configuration for the most common scenarios — forcing HTTPS, canonicalizing www vs non-www, moving to a new domain, redirecting individual pages, redirecting entire path prefixes, and handling complex regex rewrites.
Nginx supports redirects with both return and rewrite directives. The return directive is the recommended approach — it's faster, simpler, and easier to read. The rewrite directive is useful when you need regex capture groups or want to keep processing after the rewrite. This generator picks the right directive for each redirect type so the output is idiomatic and performant.
All processing happens locally in your browser. Your domain names, paths, and patterns are never sent to a server.
How to Use Nginx Redirect Rule Generator
- 1. Pick a redirect type — HTTPS, www canonicalization, trailing slash, domain move, single page, prefix, regex, or custom.
- 2. Choose a status code. Use
301for permanent moves (SEO-friendly, cacheable) and302for temporary ones. Use307/308when you need to preserve the HTTP method on POST/PUT requests. - 3. Enter the server name (your domain) and any paths or patterns the selected redirect type requires.
- 4. Decide whether to preserve the query string. For most redirects you want this enabled so UTM tags and tracking parameters survive.
- 5. Toggle Wrap in server block off if you're pasting into an existing server block, on if you want a standalone config.
- 6. Click Copy, paste into your Nginx config, run
sudo nginx -tto validate, thensudo systemctl reload nginxto apply. - 7. Verify with
curl -I https://yourdomain.com/old-path— you should see the expectedLocation:header and status code.
Common Use Cases
Force HTTPS site-wide
Redirect all HTTP traffic to HTTPS to satisfy HSTS, browser warnings, and SEO best practices.
Canonicalize www vs non-www
Pick one canonical hostname and 301 the other to prevent duplicate content and split link equity.
Rebrand or domain migration
Move thousands of URLs from old-brand.com to new-brand.com while preserving paths and SEO authority.
CMS or framework migration
Redirect old WordPress permalinks to new SvelteKit, Next.js, or Astro URLs after switching stacks.
Restructure blog or docs
Move /blog/* to /articles/* or split /docs into /guides and /reference without breaking inbound links.
Retire legacy URLs
301 deprecated landing pages to their replacements so external backlinks still convert.
Frequently Asked Questions
What's the difference between 301 and 302 redirects?
A 301 is permanent — browsers and search engines cache it aggressively and transfer link equity to the new URL. A 302 is temporary — clients keep requesting the original URL. Use 301 for moves you don't plan to undo (rebrands, restructures) and 302 for short-lived redirects (maintenance pages, A/B tests).
When should I use 307 or 308 instead?
Use 307 (temporary) or 308 (permanent) when the request method must be preserved. With 301/302, browsers may rewrite a POST to a GET on redirect. With 307/308 the original method is guaranteed — important for API endpoints and form submissions.
Should I use return or rewrite?
Prefer return. It's faster, simpler, and the Nginx docs explicitly recommend it for redirects. Use rewrite only when you need regex capture groups in the target URL or want subsequent rules to keep processing. This generator picks the right one for each redirect type.
Why does my redirect lose the query string?
Nginx doesn't preserve the query string automatically when you use return. You have to append $is_args$args to the target URL. This tool adds it for you when "Preserve query string" is enabled.
How do I redirect HTTP to HTTPS without breaking anything?
Create a dedicated server block listening on port 80 that does only one thing: return 301 https://$host$request_uri;. Then keep your real config on the HTTPS server block (port 443). Avoid mixing redirect logic into the SSL block.
Why is the if directive controversial in Nginx?
The Nginx wiki famously warns "if is evil" because nested if blocks can produce surprising behavior. However, using if ($host = ...) at the top of a server block with a single return inside is safe and idiomatic — it's the recommended pattern for www canonicalization.
How do I test redirects without affecting users?
Use curl -I https://example.com/old-path to see the response headers without following the redirect. Add -L to follow it and confirm the final destination. You can also test against a staging server before deploying to production.
Do redirects affect SEO?
Yes — but in a good way when done right. A 301 passes nearly all link equity to the new URL. Avoid redirect chains (A → B → C), don't redirect to unrelated pages, and update your XML sitemap. After deploying, request reindexing in Google Search Console for the affected URLs.
Is my data sent to any server?
No. This tool runs entirely in your browser. Your domain names, paths, regex patterns, and rule configuration never leave your device.