Nginx redirects send visitors and search engines from one URL to another, and the cleanest way to write them is a dedicated server block with return. Here is how to handle the common cases: forcing HTTPS, choosing www or non-www, and moving pages without losing SEO.
301 vs 302: pick the right status code
The status code tells browsers and search engines whether the move is permanent.
return 301: permanent. Use this for force-HTTPS, www changes, and moved pages. Search engines transfer ranking signals to the new URL, so 301 is what you want for any permanent move.return 302: temporary. Use this when the original URL will come back, like a maintenance page or a short A/B test. Search engines keep indexing the old URL.
When in doubt for a permanent change, use 301 so your SEO equity follows the new address.
Why return beats rewrite for Nginx redirects
For simple redirects, prefer return over rewrite. A return directive is faster and clearer: it sends the status code and the new location immediately, with no regular expression matching. rewrite is meant for internally remapping URLs and only redirects as a side effect, which makes it easy to introduce loops or subtle bugs. Reach for rewrite only when you genuinely need pattern capture.
Put redirects in their own server block so the matched host or scheme is handled in one place:
# Force HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
# Redirect www to non-www (already on HTTPS)
server {
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
The $request_uri variable preserves the full path and query string, so /blog?page=2 lands on the same path at the new host instead of dumping everyone on the home page.
Single path vs a whole prefix
To redirect one exact page, use a location = match:
location = /old-page/ {
return 301 /new-page/;
}
To move an entire section, match the prefix and append the rest of the path with $request_uri or a captured variable so deeper URLs keep working. The Nginx Redirect Generator writes both patterns for you, including the $request_uri handling, so you do not have to remember the exact syntax.
Generate and apply your redirect
- Open the Nginx Redirect Generator and choose your redirect type: HTTPS, www, single page, or whole prefix.
- Enter the source and destination, and pick 301 for a permanent move or 302 for a temporary one.
- Copy the generated
serverorlocationblock into your site config. - Validate the syntax with
nginx -t. - Apply it live with
nginx -s reload(orsystemctl reload nginx), which reloads without dropping connections. - Test the result with
curl -I https://www.example.com/old-page/and confirm theHTTP/.. 301status and theLocationheader point where you expect.
Always run nginx -t before reloading. A bad config that fails the test will not take down a running server, but reloading a broken file can.
Related tools
- Apache to Nginx Config Converter: migrating off Apache? Convert your rules in one pass.
- .htaccess Builder: still on Apache? Build the equivalent redirects there.
- Security Headers Generator: harden the destination with HSTS and friends.
Use 301 for permanent moves, preserve the path with $request_uri, test with nginx -t and curl -I, and your redirects will be fast and SEO-safe.