Discover how a few simple flags can save your website from broken links and lost visitors!
Why Rewrite flags (last, break, redirect, permanent) in Nginx? - Purpose & Use Cases
Imagine you manage a website and need to change many URLs manually in your server settings every time you update your site structure.
You have to edit each URL one by one in multiple places, which is confusing and takes a lot of time.
Manually updating URLs is slow and easy to mess up.
You might forget to update some links or create loops that break your site.
This causes bad user experience and lost visitors.
Rewrite flags in nginx let you control how URL changes happen automatically.
They help you decide when to stop rewriting, when to redirect users, and how to do it cleanly.
This makes your site faster and easier to manage.
if ($request_uri = "/old-page") { return 301 /new-page; } # Repeat for every URL
rewrite ^/old-page$ /new-page permanent;
# One line handles the redirect cleanlyYou can easily manage URL changes and redirects without breaking your site or confusing users.
When you redesign your website and move pages, rewrite flags help send visitors from old URLs to new ones smoothly.
Manual URL changes are slow and error-prone.
Rewrite flags automate and control URL handling.
This improves site reliability and user experience.
Practice
last flag do in an nginx rewrite directive?Solution
Step 1: Understand the
Thelastflag behaviorlastflag tells nginx to stop processing current rewrite directives and restart the rewrite phase with the new URI.Step 2: Compare with other flags
Unlikebreakwhich stops rewriting internally,lastrestarts processing, and unlikeredirectorpermanentwhich send redirects, it does not send a client redirect.Final Answer:
Stops processing the current location and restarts the rewrite phase with the new URI -> Option AQuick Check:
last= restart rewrite phase [OK]
- Confusing last with break flag
- Thinking last sends a redirect
- Mixing last with permanent redirect
Solution
Step 1: Identify the flag for permanent redirect
Thepermanentflag sends an HTTP 301 status code indicating a permanent redirect.Step 2: Check syntax correctness
The syntaxrewrite ^/old$ /new permanent;correctly applies the permanent redirect flag.Final Answer:
rewrite ^/old$ /new permanent; -> Option CQuick Check:
Permanent redirect usespermanentflag [OK]
- Using 'last' or 'break' instead of 'permanent' for redirects
- Omitting the semicolon at the end
- Confusing 'redirect' with 'permanent'
location /test {
rewrite ^/test/(.*)$ /new/$1 redirect;
}What will happen when a client requests
/test/page?Solution
Step 1: Understand the
Theredirectflagredirectflag sends an HTTP 302 temporary redirect to the client.Step 2: Analyze the rewrite rule
The rewrite matches/test/pageand rewrites it to/new/pagewith a temporary redirect.Final Answer:
The client will receive a 302 temporary redirect to /new/page -> Option DQuick Check:
redirect= 302 temporary redirect [OK]
- Confusing redirect with permanent
- Thinking redirect rewrites internally
- Assuming last or break flags here
rewrite ^/oldpath/(.*)$ /newpath/$1 last
But it causes a 500 Internal Server Error. What is the likely problem?
Solution
Step 1: Check syntax of rewrite directive
Every nginx directive must end with a semicolon. The given rewrite lacks a semicolon.Step 2: Understand error cause
Missing semicolon causes nginx to fail parsing config, leading to 500 error.Final Answer:
Missing semicolon at the end of the rewrite directive -> Option AQuick Check:
Missing semicolon = syntax error = 500 error [OK]
- Assuming last flag causes infinite loop
- Ignoring missing semicolon syntax errors
- Thinking regex pattern is wrong without checking
/app to /app/index.html internally without redirecting the client, and stop further rewrite processing. Which rewrite directive and flag should you use?Solution
Step 1: Identify internal rewrite without client redirect
Thebreakflag stops rewrite processing and serves the rewritten URI internally without redirecting the client.Step 2: Match requirement to flag
Usingbreakwith the rewrite stops further rewrites and serves/app/index.htmlinternally.Final Answer:
rewrite ^/app$ /app/index.html break; -> Option BQuick Check:
break= internal rewrite stop [OK]
- Using last which restarts rewrite phase
- Using redirect or permanent which send client redirects
- Not stopping rewrite processing causing unexpected behavior
Ready to prove it?
Pass the test and this pattern moves unknown
