Sub topic 14
Writing redirects in nginx and Apache
This chapter assumes you can read HTTP status codes, know the difference between 301 and 302, and can edit a server config file and reload the server. Here you will write one-hop permanent redirects and a custom 404 page that still returns a 404 status.
The rule: point straight at the final URL
A redirect chain forms when A redirects to B and B redirects to C. When you write a redirect, look up where the destination currently ends up. Then send the old URL directly there. Each extra hop costs the visitor a round trip and gives crawlers another chance to give up.
nginx
Use return for a simple redirect. It is faster and safer than a rewrite rule. The = in location = /old-page makes an exact match, so only that path is redirected.
# Redirect an old URL straight to its final destination (one hop)
location = /old-page {
return 301 /new-page;
}
# Serve a custom page but keep the 404 status
error_page 404 /404.html;
location = /404.html {
internal;
}The internal directive means visitors cannot request /404.html directly. Only nginx can serve it, as the response to a 404. The status stays 404 because error_page 404 /404.html; has no = code. Writing error_page 404 =200 /404.html; would turn the page into a soft 404.
Check and apply the change:
sudo nginx -t && sudo systemctl reload nginxApache
The Redirect directive comes from mod_alias. Give it the status, the old path, and the full destination URL.
# Requires mod_alias
Redirect 301 /old-page https://example.com/new-page
ErrorDocument 404 /404.htmlErrorDocument keeps the 404 status only when the target is a local path, as here. If you write a full URL such as ErrorDocument 404 https://example.com/404.html, Apache sends a redirect to that URL. The visitor then receives the error page with a 200 status.
Check and apply the change:
sudo apachectl configtest && sudo systemctl reload apache2On some systems the service is named httpd instead of apache2.
Choosing the status code
| Situation | Code |
|---|---|
| Page moved for good | 301, or 308 if the request method must be kept |
| Temporary move, such as a maintenance page | 302, or 307 to keep the method |
| Content removed with no replacement | 410, or let it return 404 |
For a removed page, nginx can answer with return 410; inside the matching location. Apache can use Redirect gone /old-page.
Verify every change
Test with curl after each reload. This is the same trace you used earlier in the tutorial.
curl -sIL https://example.com/old-page | grep -iE '^(HTTP|location)'You should see one 301 line, one Location line, and then 200. Also request a path that does not exist and confirm the status is 404, not 200.
Exercise
You set ErrorDocument 404 https://example.com/404.html in Apache. Crawlers now report the missing pages as soft 404s. What went wrong, and how do you fix it?
Show answer
With a full URL, Apache redirects the visitor to that URL, so the error page is served with a 200 status. Use a local path instead: ErrorDocument 404 /404.html. Then request a missing URL with curl and confirm it returns 404.