Write Safe NGINX Redirect & Rewrite Rules Prompt
Author correct, loop-free NGINX redirects and rewrites that preserve query strings, use the right status code, avoid open redirects, and prefer return over rewrite where possible, with test cases.
- Target user
- DevOps engineers handling migrations, canonicalization, and URL changes in NGINX
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior NGINX engineer who writes redirect and rewrite rules that don't loop, leak, or break SEO. I need correct rules for a URL change. I will provide: - The mapping I want (old → new): host canonicalization, http→https, trailing-slash policy, path moves, or a bulk old-path table - Whether redirects must be permanent (301/308) or temporary (302/307), and whether request method/body must be preserved - Existing `server`/`location` blocks and any current rewrite/return rules - Whether this fronts an app that also does its own redirects (loop risk) Your job: 1. **Pick the mechanism** — prefer `return 301 https://...$request_uri` and dedicated `server` blocks over regex `rewrite` for canonical/host redirects; reserve `rewrite ... last/break` for genuine internal path rewriting. 2. **Choose the status** — 301 vs 308 (method-preserving) and 302 vs 307, and explain caching/SEO implications of getting it wrong. 3. **Preserve correctly** — keep the query string (`$request_uri` vs `$uri`+`$args`) and avoid double-encoding; handle trailing slash consistently. 4. **Prevent loops & open redirects** — ensure http→https and host canonicalization can't ping-pong, and never build a redirect target from untrusted Host/`$arg_` input. 5. **Order & context** — place rules so they don't shadow each other or fight `try_files`, and use `merge_slashes`/`$request_uri` carefully. 6. **Verify** — give `curl -I` test cases for each mapping (with and without query string, http and https, www and apex) showing the exact Location and status. Output as: (a) the redirect/rewrite config, (b) why this mechanism and status, (c) loop/open-redirect check, (d) curl test matrix.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Fix NGINX Location Block Precedence Prompt
Untangle why the wrong NGINX location block is matching a request by tracing prefix vs regex vs exact-match precedence, then reorder/rewrite the blocks so each URL hits the intended handler.
-
Secure NGINX Reverse-Proxy Vhost Prompt
Write a hardened NGINX reverse-proxy server block that forwards correct headers, hides upstream details, sets sane security headers, and avoids common proxy footguns like open host headers and missing real-IP handling.
-
Apache .htaccess to NGINX Translation Prompt
Translate an Apache vhost or .htaccess (RewriteRules, auth, headers, directory rules) into a correct NGINX server block — preserving behavior and flagging the rules that have no clean NGINX equivalent.
-
NGINX Canary & Blue-Green Routing Prompt
Design percentage-based canary and blue-green traffic routing in NGINX using split_clients and upstream weighting — with a sticky cohort, an instant rollback path, and a way to force yourself onto the canary for testing.
More NGINX prompts & error guides
Browse every NGINX prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.