Free Tool

Free Nginx Redirect Generator

Generate Nginx 301 and 302 redirect rules for any URL change. Server blocks, location matches, and force-HTTPS or www canonicalization with correct syntax. Free, copy-paste, no signup.

to
# Nginx redirects generated by U2L AI (u2l.ai/tools/nginx-redirect-generator)

# Path redirects - place these inside your main server { } block
location = /old-page {
    return 301 /new-page;
}

Add server blocks to your config, place location blocks inside your main server block, then test with nginx -t and reload. Adjust ssl directives to match your setup.

No signup required
Free forever
GDPR compliant
Powered by U2L

Quick Answer

An Nginx redirect generator builds the server-block and location directives that send visitors from an old URL to a new one. Add your source and destination paths, choose 301 (permanent) or 302 (temporary), and optionally force HTTPS or www, and the tool outputs ready-to-paste Nginx config. The U2L Nginx Redirect Generator runs in your browser. Free, no signup.

Quick Facts

  • Nginx redirects use return and rewrite directives inside server and location blocks - not .htaccess (that is Apache only).
  • 301 is a permanent redirect (passes SEO value, browser-cached); 302 is temporary (no caching, no SEO transfer).
  • The modern, efficient pattern for whole-host redirects is a dedicated server block with return 301 https://host$request_uri.
  • Path redirects use location = /old { return 301 /new; } placed inside your main server block.
  • $request_uri preserves the original path and query string through the redirect.
  • After editing, validate with nginx -t and reload with nginx -s reload or systemctl reload nginx.
  • Browser-only and instant - rules are built locally and never sent to U2L servers.

How to generate Nginx redirects

Add redirects, pick options, copy the config.

  1. 1

    Add your redirects

    Enter each old path and the new destination (a path or full URL). Add as many location redirects as you need with the Add redirect button.

  2. 2

    Choose 301 or 302 and canonicalization

    Pick a permanent 301 or temporary 302, then optionally enable force-HTTPS and www or non-www canonicalization with your domain.

  3. 3

    Add to your config and reload

    Paste server blocks into your config and location blocks inside your main server block, run nginx -t to validate, then reload Nginx.

What is a Nginx Redirect Generator?

Nginx Redirect Generator is a tool that builds Nginx redirect configuration from a simple form. You enter old and new URLs, choose the redirect type, and the generator produces the correct server-block and location directives - so you do not have to remember Nginx's return and rewrite syntax or risk a config error that fails to reload.

Nginx is one of the most widely deployed web servers and reverse proxies. Unlike Apache, it has no .htaccess file: redirects and rewrites live in the main configuration and are loaded when Nginx starts or reloads. Redirects are expressed with the return directive (preferred for speed) or the rewrite directive (for pattern matching), inside server and location blocks.

Writing these by hand is error-prone. A common anti-pattern is using a rewrite or if inside a location when a clean return in a dedicated server block would be faster and safer - Nginx's own documentation warns that 'if is evil' in location context. A generator produces the efficient, idiomatic pattern for each job and the correct status code.

DevOps engineers, backend developers, and site owners use this when migrating URLs, forcing HTTPS, canonicalizing the www prefix, or retiring old pages on any Nginx-served site - including those behind it as a reverse proxy.

How does a Nginx Redirect Generator work?

For whole-host canonicalization, the generator emits dedicated server blocks. Force-HTTPS produces a server block listening on port 80 whose only job is return 301 https://host$request_uri - the fastest way to upgrade every insecure request. The www rules add a server block that matches the non-canonical host and returns a redirect to the canonical one, preserving the path via $request_uri.

For path redirects, the tool emits location = /old-page { return 301 /new-page; } blocks. The = modifier makes it an exact match, which is efficient and unambiguous. The destination can be a path on the same host or a full URL elsewhere. These location blocks go inside your existing main server block, which the output notes.

The generator favors return over rewrite wherever possible because return is evaluated faster and is easier to reason about - rewrite is reserved for genuine pattern transformations. It also uses $request_uri so the original path and query string survive the redirect, which is what you almost always want for migrations and HTTPS upgrades.

Everything is computed in your browser; nothing is sent to U2L. The output is standard Nginx syntax, but Nginx will not apply it until you reload. Always run nginx -t first to catch syntax errors before they affect traffic, then reload - and adjust the ssl_certificate and listen directives to match your real server setup, since those are environment-specific.

Use Cases

How marketers, businesses, and developers use nginx redirect generator.

Forcing HTTPS on an Nginx site

A dedicated port-80 server block returning 301 to https is the canonical Nginx pattern for upgrading every request to a secure connection.

Redirecting moved or renamed pages

When a URL changes, an exact-match location with return 301 keeps old links and rankings working by sending traffic to the new address.

Canonicalizing www vs non-www

Redirect the non-canonical host to your chosen one with a small server block, consolidating SEO signals and avoiding duplicate content.

Site migrations on Nginx infrastructure

Map many old URLs to new ones during a redesign so no inbound link breaks, using clean location blocks rather than fragile rewrite chains.

Reverse-proxy and microservice routing

Add redirects at the Nginx edge in front of app servers so clients are sent to canonical URLs before requests reach the backend.

Retiring old content gracefully

Send visitors from a discontinued page to the best replacement instead of a 404, preserving the visit and any link equity.

Temporary redirects for campaigns

Use return 302 to point a campaign path at a seasonal landing page, then remove it later without the redirect being cached permanently.

Replacing 'if is evil' anti-patterns

Swap fragile if blocks inside locations for clean server-block returns, the pattern Nginx itself recommends for redirects.

Consolidating http/https/www variants

Resolve duplicate-content splits across scheme and host so search engines index a single canonical version of every URL.

Nginx Redirect Generator vs Alternatives

Side-by-side feature and pricing comparison with the top alternatives.

FeatureU2LManual editingif-in-locationApache .htaccess
Free, no signup
Idiomatic return-based rulesManualN/A
Force HTTPS + www server blocksManualFragile
Preserves path with $request_uriManualOften missedDifferent syntax
Works on Nginx
Browser-only (no data sent)N/A

Nginx Redirect Generator vs Editing the Nginx config by hand

Experienced engineers edit nginx.conf or site files directly. It is fast and needs no tool once return, rewrite, and server-block patterns are familiar.

The generator helps avoid the subtle pitfalls: choosing return over rewrite, using $request_uri to preserve the path, and putting whole-host redirects in their own server block instead of an if. It produces the idiomatic pattern so a reload does not fail or behave unexpectedly.

Nginx Redirect Generator vs Using if inside a location block

Many tutorials show redirects built with if inside a location. It appears to work but Nginx documentation explicitly warns that if in location context is evil and can behave unpredictably.

This generator emits the recommended alternatives - dedicated server blocks for host-wide redirects and exact-match locations with return for paths. The result is faster and far less likely to surprise you under edge-case requests.

Best Practices

Prefer return over rewrite

return is evaluated faster and is easier to reason about. Reserve rewrite for genuine pattern transformations, not simple page-to-page redirects.

Use a dedicated server block for HTTPS

A separate port-80 server block that only returns 301 to https is the canonical, efficient pattern - cleaner than testing the scheme inside a location.

Preserve the path with $request_uri

Append $request_uri to host redirects so the original path and query string carry through. Dropping it sends every visitor to the homepage.

Validate with nginx -t before reloading

Always run nginx -t to catch syntax errors. A bad config that reloads can take the server down; nginx -t catches it safely first.

Avoid if inside location blocks

Follow Nginx's guidance and keep redirects in server blocks or exact-match locations with return, not if conditions, to prevent surprising behavior.

Use exact-match locations for single pages

location = /path is an exact match that is efficient and unambiguous, ideal for redirecting one specific URL.

Adjust ssl directives to your setup

The generated 443 server blocks are templates. Add your real ssl_certificate and ssl_certificate_key paths to match your environment.

Reload, do not restart, for zero downtime

Apply changes with nginx -s reload or systemctl reload nginx so existing connections are not dropped while new config takes effect.

Common Mistakes to Avoid

Dropping the path with a bare host redirect

return 301 https://example.com; sends every URL to the homepage. Append $request_uri to keep the original path and query string.

Using if inside a location

Redirects built with if in location context can misbehave on edge cases. Use dedicated server blocks or exact-match locations with return instead.

Reloading without running nginx -t

Pushing an invalid config can fail the reload and disrupt the server. Validate with nginx -t every time before reloading.

Using 302 for permanent moves

A temporary 302 does not pass SEO value and is not cached, so search engines keep the old URL. Use 301 for permanent redirects.

Expecting .htaccess to work on Nginx

Nginx ignores .htaccess entirely. Redirect rules must live in the Nginx config. Copying Apache rules over does nothing.

Putting location blocks outside a server block

location directives only work inside a server block. Pasting them at the top level of the config produces an error on reload.

Technical Specifications

ConfigNginx server and location blocks (no .htaccess)
Path redirectlocation = /old { return 301 /new; }
Host redirectreturn 301 https://host$request_uri;
301Permanent - passes SEO value, browser-cached
302Temporary - no SEO transfer, not cached
Path preservation$request_uri keeps the original path + query
Apply changesnginx -t to validate, then reload Nginx
PrivacyBuilt in your browser. No data sent to U2L.

Frequently Asked Questions

How do I create a redirect in Nginx?

Use the return directive. For a single page: location = /old-page { return 301 /new-page; } inside your server block. For a whole host: a server block with return 301 https://example.com$request_uri. The generator builds both.

What is the difference between 301 and 302 in Nginx?

A 301 is permanent - it passes SEO value to the new URL and browsers cache it. A 302 is temporary - no SEO transfer and not cached. Choose 301 for permanent moves and 302 only for genuinely temporary redirects.

Should I use return or rewrite for redirects?

Prefer return. It is faster and clearer for simple redirects. Use rewrite only when you need pattern matching or capture groups to transform the URL. The generator uses return for the redirects it builds.

How do I force HTTPS in Nginx?

Add a server block listening on port 80 that does return 301 https://$host$request_uri (or your fixed host). Enable the force-HTTPS option and the generator outputs exactly this efficient pattern.

How do I redirect www to non-www (or the reverse)?

Add a small server block that matches the non-canonical host and returns 301 to the canonical one with $request_uri. Choose Force www or Force non-www and enter your domain to generate it.

Why does my redirect send everything to the homepage?

You probably omitted $request_uri. A redirect like return 301 https://example.com; drops the path. Append $request_uri so the original path and query string are preserved.

Where do I put location redirect blocks?

Inside your main server block - the one handling your site. location directives are only valid within a server block; placing them at the top level of the config causes an error on reload.

How do I apply the new redirects?

Save the config, run nginx -t to validate the syntax, then reload with nginx -s reload or systemctl reload nginx. Reloading applies changes without dropping active connections.

Why is using if for redirects discouraged?

Nginx's documentation states that if inside a location context is 'evil' because it can behave unpredictably. The recommended approach - which this generator uses - is dedicated server blocks and exact-match locations with return.

Does Nginx use .htaccess?

No. .htaccess is an Apache feature and Nginx ignores it completely. All redirects and rewrites must be defined in the Nginx configuration files. If you are on Apache, use the .htaccess Redirect Generator instead.

What does $request_uri do?

$request_uri is the full original request path including the query string. Including it in a redirect target preserves what the visitor asked for, so /old-page?ref=x lands on the matching new path rather than the root.

Can I redirect to a different domain?

Yes. The destination in return can be a path on the same host or a full URL on another domain. Enter the absolute URL and the generator places it in the return directive.

Do Nginx redirects affect SEO?

Yes - a 301 transfers most of the old URL's ranking value to the new one. Using the right status code, preserving the path, and avoiding redirect chains all help SEO, which is why correct config matters.

What is an exact-match location?

location = /path uses the = modifier for an exact match on that URL only. It is efficient and unambiguous, making it ideal for redirecting one specific page.

Is this generator free and private?

Yes. It is free with no signup, and the config is built entirely in your browser. Your domains and paths never leave your device.

How do I test that a redirect works?

After reloading, load the old URL or use a redirect checker to confirm it returns the expected 301 or 302 and lands on the right destination in one hop. U2L's Redirect Checker can trace the full chain.

Key Terms

return directive
The Nginx directive that issues a redirect (or fixed response) directly, the preferred and fastest way to redirect a request.
rewrite directive
An Nginx directive for pattern-based URL transformation, used when a redirect needs regex matching rather than a fixed target.
server block
A configuration block defining how Nginx handles requests for a given host and port - the right place for host-wide redirects.
$request_uri
An Nginx variable holding the full original request path and query string, used to preserve the path through a redirect.
exact-match location
A location block using the = modifier (location = /path) that matches one specific URL, efficient for single-page redirects.

Need redirects without touching server config?

U2L lets you create and edit redirects as branded short links - no server reload, no config files, full click analytics, and editable destinations any time. Sign up free to manage redirects the easy way.

Sign up free