Nginx Rewrite Rule Generator

Updated October 2026

Generate Nginx rewrite and redirect rules the way Nginx wants them: location + return for redirects, rewrite with last or break for internal rewrites, server blocks for HTTPS and www, with the query string handled and a URL tester. Free, runs in your browser.

Advertisement

Advertisement

1. Start from a recipe

/old-blog/anything → /blog/anything.

2. The rule

Directive style

Your Nginx configuration

location ~ ^/old-blog/(.*)$ {
    return 301 /blog/$1$is_args$args;
}

What it does, line by line

  1. location ~ matches the request path against the regex; parentheses become $1, $2...
  2. return 301 redirects to /blog/$1 and $is_args$args keeps the original query string. return is cheaper than rewrite and is what the Nginx docs recommend for redirects.

Test on a URL

Matches. The visitor is redirected to https://www.example.com/blog/my-post?utm_source=newsletter

How Nginx Rewrites Differ From .htaccess Rules

Nginx has no per-directory .htaccess; everything lives in the server configuration (nginx.conf or a file under sites-available) and takes effect after nginx -t and a reload. Three things trip up people coming from Apache: the regex is matched against the URI with its leading slash (^/old, not ^old); the query string is never part of the match (use $args or $arg_name for that); and for plain redirects the recommended directive is not rewrite at all but return inside a location block. The generator above follows those conventions and lets you switch between the two styles.

return vs. rewrite: Which Nginx Directive to Use

DirectiveBest forWhy
location = /old { return 301 /new; }One exact URLNo regex is evaluated; the fastest form.
location ~ ^/old/(.*)$ { return 301 /new/$1; }A pattern of URLs that redirectreturn stops processing immediately; the Nginx docs prefer it over rewrite for redirects.
rewrite ^/old/(.*)$ /new/$1 permanent;The same, when the rule must live in the server block before all locationsWorks, but Nginx keeps evaluating rewrite directives in the same context.
rewrite ^/p/(\d+)$ /product.php?id=$1 last;Internal rewrite that changes which location handles the requestlast restarts the location search with the new URI; the browser URL does not change.
rewrite ... break;Internal rewrite that stays in the current locationServes the new URI as a file from this location; no new location search.

permanent and redirect are the rewrite flags for 301 and 302. Without a flag, a rewrite whose replacement starts with http:// or https:// is still an external 302 redirect.

Nginx Redirect and Rewrite Examples

Force HTTPS

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

A dedicated port-80 server block is cleaner and faster than an if ($scheme) test inside the main block. $request_uri is the original URI including the query string, so nothing is lost.

www to non-www

server {
    listen 443 ssl;
    server_name www.example.com;
    # certificate must cover www.example.com
    return 301 https://example.com$request_uri;
}

Swap the names for non-www to www. Add a matching port-80 block so http://www also lands in one hop.

Move a folder

location ~ ^/old-blog/(.*)$ {
    return 301 /blog/$1$is_args$args;
}

$is_args$args re-attaches the query string, because return with a path (unlike rewrite) does not do it automatically.

Remove the trailing slash

rewrite ^/(.+)/$ /$1 permanent;

Keep the root (/) out of it: the .+ requires at least one character before the slash.

Remove .html from URLs

if ($request_uri ~ ^/(.+)\.html(\?.*)?$) {
    return 301 /$1$is_args$args;
}
location / {
    try_files $uri $uri.html $uri/ =404;
}

Testing $request_uri (the raw request) rather than $uri is what prevents a loop with try_files, which internally rewrites /page to /page.html.

Query string to a clean URL

location = /product.php {
    if ($arg_id ~ "^([0-9]+)$") {
        return 301 /product/$1;
    }
}

Clean URL served by a script

rewrite ^/product/([0-9]+)/?$ /product.php?id=$1 last;

"If Is Evil": When if Is Safe in Nginx

The Nginx wiki's warning about if refers to using it inside a location with directives other than return or rewrite ... last, where it can produce broken configurations. An if that only contains a return is explicitly safe, which is why the query-string and .html recipes above use it. Everything else should be expressed with location blocks, map or try_files.

Checking an Nginx Rewrite

  1. Paste the generated block into the right server block (location blocks inside it; redirect server blocks beside it).
  2. Run nginx -t. It catches syntax errors and unknown directives before anything goes live.
  3. Reload: systemctl reload nginx (or nginx -s reload).
  4. Request an old URL with curl -I or the broken link checker: expect 301 and the right Location, then a 200 on the target.
  5. For the same rules on Apache, use the .htaccess RewriteRule generator or, for plain redirects, the Nginx 301 redirect generator and .htaccess 301 redirect generator.

How to Use

  1. 1

    Pick a recipe: force HTTPS, www or non-www, move a folder, exact page, trailing slash, remove .html or .php, query string to clean URL, or an internal rewrite

  2. 2

    Adjust the regex and the target ($1, $2 reuse captured parts) and choose 301, 302, last or break

  3. 3

    For redirects, keep the recommended location + return style or switch to a rewrite directive

  4. 4

    Test a URL to see the redirect target or the internally served URI

  5. 5

    Copy the block into your server configuration, run nginx -t, then reload Nginx

Why This Tool Matters for SEO

Nginx rewrites look like Apache rules but behave differently: the regex sees the leading slash, the query string is separate, return beats rewrite for redirects, and a misplaced if can break a server block. Most redirect loops and 404s after a migration to Nginx come from copying .htaccess logic verbatim. Generating the directive in the Nginx idiom, with the query string handled and the rule tested on a URL, avoids that.

Frequently Asked Questions

Should I use return or rewrite for an Nginx redirect?

return. Inside a location block, return 301 /new$is_args$args; is evaluated without a regex engine and stops processing immediately; the Nginx documentation recommends it over rewrite for any redirect that does not need captured groups beyond what the location regex provides. Use rewrite ... permanent when the rule must sit in the server block before the location search, and rewrite ... last or break for internal rewrites.

How do I force HTTPS in Nginx?

With a separate server block for port 80 that only redirects: server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; }. $request_uri carries the full original path and query string. Do not use if ($scheme = http) inside the main block.

How do I redirect www to non-www in Nginx?

Give www its own server blocks (port 80 and 443) that return 301 https://example.com$request_uri;, and serve the site only under the bare domain. The 443 block for www needs a certificate that covers www.example.com. The generator writes all three blocks.

Why does my Nginx rewrite not keep the query string?

rewrite appends the original query string automatically (unless the replacement ends with ?), but return with a path does not. Add $is_args$args to the return target: return 301 /new$is_args$args;. $request_uri already includes the query string, which is why the HTTPS and www recipes use it.

What is the difference between last and break?

Both stop the current set of rewrite directives. last then restarts the search for a location block that matches the new URI, which is what you need to hand /product/42 to the PHP location as /product.php?id=42. break keeps the request in the current location and serves the new URI from there, typically as a static file.

Is it safe to use if in Nginx configuration?

Only when the if block contains nothing but return or rewrite ... last. That is why the query-string and remove-.html recipes use if with a single return. Any other directive inside an if in a location context can behave unexpectedly; use location blocks, map or try_files instead.

Related Tools You Might Like

More in Technical SEO tools · All free SEO tools

Local Processing: Your data never leaves your browser