What if you could fix broken links instantly without changing a single line of your website code?
Why Redirect and rewrite rules in Firebase? - Purpose & Use Cases
Imagine you have a website and you want to change some page addresses or send visitors to new pages without breaking links.
You try to do this by manually changing every link in your code and telling your team to update bookmarks.
This manual way is slow and confusing.
Itβs easy to miss some links, causing visitors to see errors or old pages.
Every time you want to change something, you must hunt through your code and fix many places.
Redirect and rewrite rules let you set simple instructions in one place.
These rules automatically send visitors to the right pages or change URLs behind the scenes.
This keeps your site working smoothly without changing every link manually.
Change every link in HTML and JavaScript files manually
firebase.json with redirect and rewrite rules configured"
You can easily manage URL changes and user navigation without touching your entire website code.
Your company rebrands and changes product page URLs.
Instead of updating thousands of links, you add redirect rules so old URLs automatically send visitors to new pages.
Manual URL changes are slow and error-prone.
Redirect and rewrite rules automate URL management.
This keeps websites user-friendly and easy to update.
Practice
Solution
Step 1: Understand redirect behavior
A redirect sends the user to a new URL and updates the browser address bar to show this new URL.Step 2: Understand rewrite behavior
A rewrite serves different content but keeps the original URL visible in the browser address bar.Final Answer:
Redirect changes the browser URL, rewrite does not change the URL shown. -> Option AQuick Check:
Redirect updates URL, rewrite keeps URL same [OK]
- Confusing redirect with rewrite behavior
- Thinking rewrite changes browser URL
- Assuming redirect does not update URL
/old-page to /new-page?Solution
Step 1: Identify redirect syntax
Redirects require a source, destination, and a type (like 301 for permanent redirect).Step 2: Check options for correct syntax
"redirects": [{"source": "/old-page", "destination": "/new-page", "type": 301}] correctly includes source, destination, and type 301. Others miss type or use rewrites incorrectly.Final Answer:
"redirects": [{"source": "/old-page", "destination": "/new-page", "type": 301}] -> Option DQuick Check:
Redirect needs type code like 301 [OK]
- Omitting the redirect type code
- Using rewrites instead of redirects for URL change
- Placing type in rewrites block
{
"rewrites": [
{"source": "/app/**", "destination": "/index.html"}
]
}What happens when a user visits
/app/profile?Solution
Step 1: Understand rewrite with wildcard
The rewrite rule matches any URL starting with /app/ and serves /index.html content without changing the URL.Step 2: Apply to /app/profile
Visiting /app/profile triggers the rewrite, so content from /index.html is served but URL remains /app/profile.Final Answer:
The browser URL stays /app/profile but serves content from /index.html. -> Option AQuick Check:
Rewrite serves content without URL change [OK]
- Thinking rewrite changes the URL
- Expecting 404 error for unmatched files
- Assuming redirect happens instead of rewrite
{
"redirects": [
{"source": "/home", "destination": "/index.html"}
]
}But the redirect does not work as expected. What is the problem?
Solution
Step 1: Analyze the redirect behavior
A redirect requires a type (301 or 302) to work properly. Missing the type causes the redirect to fail.Step 2: Identify the likely expectation and fix
Adding the redirect type (301 or 302) will fix the redirect. Using rewrites is an alternative if you want to serve content without changing the URL.Final Answer:
Missing the redirect type (301 or 302) in the redirect rule. -> Option CQuick Check:
Redirect requires type code [OK]
- Using redirects instead of rewrites to preserve URL
- Confusing redirects with rewrites
- Using relative paths incorrectly
/app/ show index.html content without changing the URL, but also redirect /old-app permanently to /app/. Which config snippet correctly achieves this?Solution
Step 1: Redirect /old-app to /app/ permanently
Use a redirect with type 301 from /old-app to /app/ to update browser URL.Step 2: Rewrite all /app/** URLs to serve index.html without changing URL
Use a rewrite rule matching /app/** to serve /index.html content while keeping URL unchanged.Step 3: Verify option correctness
{ "redirects": [ {"source": "/old-app", "destination": "/app/", "type": 301} ], "rewrites": [ {"source": "/app/**", "destination": "/index.html"} ] } correctly combines a 301 redirect and a rewrite for SPA support. Others misuse rewrites or redirect types.Final Answer:
{ "redirects": [ {"source": "/old-app", "destination": "/app/", "type": 301} ], "rewrites": [ {"source": "/app/**", "destination": "/index.html"} ] } -> Option BQuick Check:
Redirect updates URL, rewrite serves SPA content [OK]
- Using rewrite instead of redirect for URL change
- Missing redirect type or using wrong type
- Rewriting to wrong destination path
Ready to prove it?
Pass the test and this pattern moves unknown
