Nginx Rewrite Rule Generator

Visually build Nginx rewrite and redirect rules. Configure regex path matches, permanent 301/302 redirects, and conditional server checks. 100% client-side, zero data uploads.

Advertisement

🔧 Rule Configuration

Use (.*) to capture sub-paths; reference as $1, $2 in the destination.

Conditional Check (if block)

How ZeroData protects your privacy

  • No Uploads: Processing happens entirely via client-side JavaScript.
  • No Storage: We do not have a database. We physically cannot save your data.
  • No Tracking: We don't log what you process or track your inputs.
  • Verifiable: Check your DevTools Network tab. You will see 0 outbound requests.

What Are Nginx Rewrite Rules and Why Do They Matter?

Nginx rewrite rules are server-side directives that intercept incoming HTTP requests and transform the URI before the web server decides how to handle them. They are one of the most powerful features in Nginx configuration, enabling everything from simple page redirects to complex URL rewriting logic using Perl-compatible regular expressions (PCRE).

Whether you are migrating a legacy PHP application to a modern framework, consolidating multiple domains, or enforcing HTTPS across your entire site, rewrite rules are the mechanism that keeps your links working and your SEO rankings intact during the transition.

Understanding Core Nginx Directives

  • Rewrite (rewrite pattern replacement [flag]): Modifies the client's request URI according to regular expressions. Supports internal parsing loops or browser redirects. Evaluated top-to-bottom within a server or location block.
  • Return (return code [text/url]): Returns an immediate HTTP status directly to the browser (such as 301, 302, 403, or 500). Nginx documentation recommends return over rewrite for simple redirects because it skips regex execution blocks for maximum throughput.
  • Redirect Flags:
    • permanent returns HTTP 301, instructing search engines and browsers to permanently cache the new URL and transfer link equity.
    • redirect returns HTTP 302, used for temporary migrations where the original URL may return.
    • last stops rewrite processing and forces Nginx to scan for a new matching location block with the modified URI.
    • break stops rewrite processing and serves content from the current location block without starting a new location search.

Capture Groups and Dynamic URL Rewriting

Nginx rewrite rules support PCRE capture groups. Enclose sub-expressions in parentheses in your match pattern (e.g., ^/blog/(.*)$), then reference them as $1, $2, etc. in the destination string. This enables powerful wildcard redirects — for example, redirecting an entire legacy blog directory to a new path while preserving all post slugs: rewrite ^/blog/(.*)$ /articles/$1 permanent;

The 'if' Directive in Nginx — Use With Caution

Nginx's if directive allows conditional execution of rewrite rules based on server variables like $host, $scheme, or $http_user_agent. However, the Nginx community has a well-known guideline: "if is evil." Nested if blocks are not supported and produce configuration errors. Always test with nginx -t before reloading. This generator strictly produces flat, single-level if blocks to ensure valid output.

SEO Best Practices for Nginx Redirects

For search engine optimization (SEO), always use 301 permanent redirects when consolidating domains (like forwarding non-www traffic to www) or restructuring site directories permanently. This ensures all existing link equity passes cleanly to the new target URLs. Use 302 redirects only when the move is temporary — search engines do not transfer link authority for temporary redirects. After deploying redirects, validate them in Google Search Console to confirm the URL change has been processed correctly.

How to Apply Generated Rules to Your Nginx Server

Copy the generated rule and open your Nginx virtual host config file (typically at /etc/nginx/sites-available/yourdomain). Paste the rule inside the appropriate server or location block. Then run sudo nginx -t to validate the syntax. If the test reports success, apply the change with sudo systemctl reload nginx. Never skip the nginx -t step — a syntax error will prevent Nginx from reloading and may take your site offline.

How to Use the Nginx Rewrite Rule Generator

  1. Select a configuration template or choose 'Custom Redirect / Rewrite'.
  2. Enter the incoming URL pattern/regex and the target destination path.
  3. Select the redirect type (301 Permanent, 302 Temporary, or internal Nginx rewrites like Last/Break).
  4. Optionally enable case-insensitive matching using the ~* operator.
  5. Add an optional conditional 'if' block to match on variables like $host or $scheme.
  6. Copy the generated block and paste it directly into your server block config.
  7. Run 'sudo nginx -t' to verify syntax, then reload Nginx.

Common Use Cases

  • Enforcing HTTPS by redirecting all HTTP requests to secure SSL connections.
  • Redirecting traffic from www to non-www domains (or vice versa) for consistent domain SEO.
  • Mapping legacy website URLs to a new directory hierarchy using regex variables.
  • Securing sensitive config folders by returning immediate 403 Forbidden statuses.
  • Performing wildcard redirects after restructuring a website's URL hierarchy.
  • Blocking crawlers or bots by matching on the $http_user_agent variable.
Advertisement

Frequently Asked Questions

What is an Nginx rewrite rule?

An Nginx rewrite rule is a directive that changes the request URI using regular expressions. It allows you to redirect old URLs to new ones, mask real internal file structures, or enforce SEO-friendly URL patterns without exposing the backend directories.

What is the difference between 'return' and 'rewrite' in Nginx?

The 'return' directive is simpler and faster because it stops processing immediately and sends a status code (like 301 or 302) or text to the client. The 'rewrite' directive is more complex; it uses regular expressions to modify the URI internally and can either perform a redirect or pass the modified URI further down the server configuration.

What do the flags 'last', 'break', 'redirect', and 'permanent' mean?

1) 'last' stops processing the current rewrite directives and starts a new search for a matching location block with the rewritten URI. 2) 'break' stops processing rewrite directives but continues executing within the current location block. 3) 'redirect' returns a temporary 302 redirect to the browser. 4) 'permanent' returns a permanent 301 redirect.

Are my redirect rules processed on your server?

No. All configuration logic and text generation happen 100% in your browser. No domains, redirect paths, or regex strings are ever uploaded or transmitted.

Where do I paste the generated rules in my Nginx config?

You typically paste these rules inside a 'server' block or a specific 'location' block within your Nginx virtual host configuration file (usually found in /etc/nginx/sites-available/). Remember to run 'nginx -t' to test the syntax before reloading Nginx.

Can Nginx rewrite rules use capture groups?

Yes. In the match pattern, wrap sub-expressions in parentheses (e.g. ^/old/(.*) ) and reference them in the destination as $1, $2, etc. For example: rewrite ^/blog/(.*)$ /posts/$1 permanent; moves all blog posts to the /posts/ path.

How do I test my Nginx configuration before applying it?

After pasting your generated rules into the config file, always run 'sudo nginx -t' to validate the syntax. If the test passes, reload the service with 'sudo systemctl reload nginx' or 'sudo nginx -s reload'.

Related Tools

© 2026 ZeroData Tools. All rights reserved.