Skip to content
empinet.dev
Generators & Identifiers

Nginx Redirect Generator — 301 & 302 Rules

Create Nginx redirect snippets for HTTP to HTTPS, canonical www or non-www hostnames, domain moves, and exact page URLs. Copy a focused server-block rule with an explanation of its scope and query behavior.

Build an Nginx redirect

Redirect settings

Redirect type

HTTP status

Use 302 while testing. Browsers may cache 301 redirects.

Source hostname

Hostname only. No scheme, port, or path.

Nginx snippet

Place at server level inside the existing server block for example.com. It preserves the original path and query string. Configure server_name, listeners, and TLS certificates separately. This assumes Nginx receives the original connection scheme directly.

Add the snippet to an existing server block

Nginx does not use .htaccess. Put hostname rules at server level inside the existing server block for the source hostname, not inside a location. Page snippets are exact-match location blocks and also belong inside server. Keep your server_name, listen directives, certificates, and existing application settings. Do not duplicate an existing exact-match location; merge its behavior deliberately.

Why use return for redirects?

A return directive directly sends the selected HTTP status and destination. These hostname conditions contain only a return, not a general rewrite program. HTTPS rules check for an HTTP connection; www and domain rules check the source hostname. Destinations are fixed rather than built from an arbitrary request Host header, and $request_uri preserves the original path and query string.

Redirect one URL without matching its children

The generated location = /old-page matches that exact normalized path, not /old-page/ or descendants. Nginx normalizes request paths before location matching, so test encoded or repeated-slash requests separately. When the target has no query, $is_args$args retains the incoming query string. When it supplies a query, the generated return uses that query instead.

Permanent versus temporary redirects

A 301 redirect signals a permanent move; 302 signals a temporary destination. Check the rule with 302 first to avoid retaining a mistaken permanent redirect in browser caches. This form does not generate 307 or 308 method-preserving redirects. Review API and form requests separately if a redirect must retain the original HTTP method and body.

TLS certificates and proxy configuration

Hostname redirects point to HTTPS, but this tool does not configure HTTPS listeners or certificates. Redirecting an HTTPS source requires a valid certificate there before the browser can receive a redirect. Behind a TLS-terminating proxy, $scheme reflects Nginx’s own connection, not necessarily the browser’s. Adapt detection only within a trusted proxy setup, or enforce the redirect at the edge.

Test before reloading Nginx

Back up the configuration, merge the snippet, run nginx -t on your server, and reload only after the test succeeds. Then check actual response status, Location, and query preservation. The generator makes no network requests and cannot test server-block selection, location conflicts, certificates, or redirect loops in your installation.

nginx -t

Reference: Nginx return directive.