Lesson 6 of 9 · 26m
Rewrites, Redirects, and Routing Rules That Don't Bite
Use redirects and rewrites for distinct jobs, keep rule order explicit, and avoid the regex and loop mistakes that make web server configuration feel haunted.
Rewrite rules earn their bad reputation because they sit at the intersection of regex, implicit ordering, URL encoding, and human optimism. The day-one rule is simple: a redirect changes what the client asks for; a rewrite changes how the server internally handles the request. If you confuse those, you will create loops, broken canonical URLs, or application routes that only work by accident.
Prefer explicit routing over clever regex
Nginx location matching is already a routing system. Use it before reaching for rewrite:
location = /old-login {
return 301 /login;
}
location /docs/ {
try_files $uri $uri/ /docs/index.html;
}
location /app/ {
proxy_pass http://app_backend/;
}return is cheap and explicit. try_files expresses fallback order. proxy_pass routes upstream. A pile of rewrite directives often means the routing model was never written down.
Apache rewrite debugging starts with the simplest rule
RewriteEngine On
RewriteRule ^/old-login$ /login [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/docs/ /docs/index.html [L]R=301 sends the client elsewhere. Without R, the substitution is internal. L stops this round of rewriting, not necessarily all rewriting forever; Apache can loop internally until limits intervene. Read the documentation for per-server versus per-directory context because they are not the same execution model.
Canonicalization is a business decision
server {
listen 443 ssl;
server_name sudosecurity.org;
return 301 https://www.sudosecurity.org$request_uri;
}Choose one canonical host and redirect the alternates. Preserve path and query unless intentionally collapsing URLs. Lowercase normalization, trailing slash behavior, and locale prefixes affect SEO, cookies, OAuth redirect URIs, and cache keys. They are not cosmetic details.
Beware encoding and matching scope
The path /search?q=%2Ftmp includes a query string, but rewrite matching often operates on the normalized path component rather than the raw full URL. Encoded slashes, backslashes, semicolon path parameters, and Unicode normalization differ across servers and frameworks. Test the exact bytes on the wire:
curl -v --path-as-is 'https://app.sudosecurity.org/search/%2e%2e%2fadmin?q=1'--path-as-is prevents curl from normalizing away the interesting part. Your browser is not a reliable test harness for this class of behavior.
Rewrite loops have operational signatures
Symptoms include 500s after internal rewrite limits, alternating 301/302 responses, infinite client redirect chains, and requests that work with curl but fail in browsers due to cached permanent redirects. Use temporary 302s while validating migration behavior. Promote to 301 only when you are confident, because browsers cache permanent redirects aggressively.
Make rules reviewable
Good rules have comments explaining intent, not mechanics. Group related redirects, order specific matches before broad fallbacks, and test with a matrix of URLs. If nobody can predict the output without reading three nested includes, the config is already too clever.
Field notes
- Nginx
rewriteinsidelocationcan interact withtry_filesand proxying in ways that surprise even experienced operators; prefer simpler constructs. - Apache
.htaccessadds distributed configuration complexity and performance cost; centralize rules when you control the server. - Never use rewrites as an authorization boundary.
/adminrewritten or hidden is still reachable if routing is not enforced server-side. - Keep a redirect inventory for migrations; forgotten legacy URLs are both user-facing breakage and a security-relevant source of open redirect confusion.
Was this lesson helpful?