How to Fix Redirects in Apache / nginx
Raw Apache and nginx redirects offer maximum control but with corresponding complexity. Regex pitfalls cause loops; flag mismatches cause wrong status codes. This guide covers Apache and nginx redirect patterns. Pair with redirect checker guide.
Step-by-step: How to fix redirects in Apache / nginx
- Choose correct redirect directive. Apache: Redirect (simple), RedirectMatch (regex), RewriteRule (full regex + flags). nginx: return (recommended), rewrite (less efficient). For SEO-critical: prefer Apache Redirect/RedirectMatch and nginx return — simpler, faster, fewer pitfalls than RewriteRule/rewrite.
- Write simple redirects first. Apache: 'Redirect 301 /old-page /new-page'. nginx: 'location = /old-page { return 301 /new-page; }'. Exact-match simple — no regex needed for one-to-one redirects.
- Write regex carefully. Apache RewriteRule: '^old-pattern/(.*)$ /new-pattern/$1 [R=301,L]'. nginx rewrite: 'rewrite ^/old-pattern/(.*)$ /new-pattern/$1 permanent;'. Common pitfalls: forgetting ^ start anchor (matches anywhere), forgetting $ end anchor (matches partial), greedy quantifiers (.+ vs .+?) causing wrong captures.
- Use correct flags. Apache: R=301 (permanent) vs R=302 (temporary). L (last — stop processing). NC (case-insensitive if needed). nginx: 'permanent' (301) vs 'redirect' (302). Forget L and rules cascade; intended single redirect becomes a chain.
- Prevent redirect loops. Test: does the rule's destination match its own pattern? If 'rewrite ^/foo/(.*)$ /bar/$1 permanent;' and /bar pattern also redirects, loops. Add condition guards (RewriteCond in Apache) or restructure patterns.
- Test before deploying. Apache: 'apachectl configtest'. nginx: 'nginx -t'. Both validate syntax. Beyond syntax: test redirects with curl -I -L from staging environment. Validate all critical SEO redirects before production deploy.
- Deploy with rollback ready. Backup current config. Deploy new. Test immediately with curl. Have rollback command ready (cp old.conf current.conf && nginx -s reload). Production redirect bugs cause outages or SEO disasters; rollback speed matters.
🟧 Audit Apache/nginx redirects
Test redirect rules on your Apache or nginx config.
Run Redirect Audit →Frequently Asked Questions
Apache RewriteRule vs Redirect — when to use which?
Redirect/RedirectMatch — simple, no regex (or simple regex), no flags. Use for one-to-one redirects. RewriteRule — full regex with capture groups, flags (R, L, NC, etc.), conditions (RewriteCond). Use for complex patterns. Both work for SEO redirects; choose simplest sufficient. Avoid mixing both for same URLs (creates ordering complexity).
nginx return vs rewrite — what's the difference?
return 301 /new-path — immediate, efficient, recommended. rewrite ^/old$ /new permanent — regex-based, slower because nginx re-evaluates the URL against location blocks after rewrite. For simple redirects, prefer return. For pattern matching with captures, rewrite is necessary.
How do I redirect with query strings preserved?
Apache: query strings preserved automatically unless rule sets QSA (query string append) or QSD (discard). nginx: rewrite preserves; return preserves only with $request_uri. Verify with curl: 'curl -I -L https://yoursite.com/old?param=value'. Confirm new URL contains param=value.
Common Apache RewriteRule mistakes?
Forgetting ^ anchor (rule matches partial URL anywhere). Forgetting $ end anchor. Greedy .* causing wrong capture. Missing L flag (rule fires but processing continues, possibly triggering more rules). Wrong R= code (302 instead of 301 for permanent). Order matters — rules evaluated top-to-bottom.
Best practices for managing large numbers of redirects?
Apache: RewriteMap with external file (txt:/path/to/map.txt). nginx: include file with returns. Both: separate redirect rules from other config in dedicated file (redirects.conf). For 10,000+ redirects: consider application-layer redirect handling (faster, more flexible than web-server rules).