Challenge - 5 Problems
Rewrite Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output status code after applying this rewrite?
Given the following nginx rewrite directive inside a server block:
What HTTP status code will the client receive when requesting
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
What HTTP status code will the client receive when requesting
/oldpath/page1?Nginx
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;Attempts:
2 left
💡 Hint
The keyword 'permanent' in rewrite directive sets a specific HTTP status code.
✗ Incorrect
The 'permanent' flag in nginx rewrite sends a 301 Moved Permanently status code to the client, indicating the resource has moved permanently to the new URL.
❓ Configuration
intermediate2:00remaining
Which rewrite rule correctly redirects /blog to /news preserving the rest of the path?
You want to redirect all requests starting with
/blog to /news, keeping the rest of the URL path intact. Which rewrite directive achieves this?Attempts:
2 left
💡 Hint
Use a regex to capture the rest of the path and reuse it in the target.
✗ Incorrect
Option C uses a regex to capture everything after /blog/ and appends it to /news/, preserving the path. The 'permanent' flag sends a 301 redirect.
❓ Troubleshoot
advanced2:00remaining
Why does this rewrite not redirect as expected?
Given this nginx config snippet:
Requests to
location /test/ {
rewrite ^/test/(.*)$ /prod/$1;
}Requests to
/test/page do not redirect to /prod/page. Why?Attempts:
2 left
💡 Hint
Rewrite without flags performs internal rewrite, not external redirect.
✗ Incorrect
Without a redirect flag like 'permanent' or 'redirect', nginx rewrites the URI internally without telling the client to change URL, so the browser URL stays the same.
🔀 Workflow
advanced2:00remaining
Order the steps to apply a rewrite rule and test it successfully
Arrange these steps in the correct order to add and verify a rewrite rule in nginx:
Attempts:
2 left
💡 Hint
Always test config before reload, then verify with a request.
✗ Incorrect
First add the rewrite, then test syntax, reload nginx to apply, finally test with a request.
✅ Best Practice
expert2:00remaining
Which rewrite directive is best to permanently redirect and avoid redirect loops?
You want to permanently redirect
/old to /new only if the request is exactly /old. Which rewrite directive is best to avoid redirect loops and unnecessary redirects?Attempts:
2 left
💡 Hint
Match exact path to prevent redirect loops.
✗ Incorrect
Option D matches exactly '/old' and redirects to '/new' with a 301 status. Other options either match more paths or are invalid, risking loops or wrong redirects.