0
0
Firebasecloud~10 mins

Redirect and rewrite rules in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Redirect and rewrite rules
User Request URL
Check Redirect Rules
| No
Check Rewrite Rules
| No
Serve Original Content
End
When a user requests a URL, Firebase first checks redirect rules to send a redirect response if matched. If no redirect applies, it checks rewrite rules to serve different content without changing the URL. Otherwise, it serves the original content.
Execution Sample
Firebase
redirects:
  - source: /old-page
    destination: /new-page
    type: 301
rewrites:
  - source: /app/**
    destination: /index.html
This config redirects '/old-page' to '/new-page' with a 301 status, and rewrites any '/app/...' URL to serve 'index.html' without changing the URL.
Process Table
StepUser Request URLRedirect Rule Match?Redirect ActionRewrite Rule Match?Rewrite ActionFinal Response
1/old-pageYesSend 301 redirect to /new-pageNoN/ARedirect to /new-page
2/app/dashboardNoN/AYesServe /index.html contentServe /index.html content with URL /app/dashboard
3/aboutNoN/ANoN/AServe original /about content
4/old-page/subNoN/ANoN/AServe original /old-page/sub content
5/appNoN/AYesServe /index.html contentServe /index.html content with URL /app
6/new-pageNoN/ANoN/AServe original /new-page content
7/old-pageYesSend 301 redirect to /new-pageNoN/ARedirect to /new-page
💡 Execution stops after sending redirect or serving content based on matched rules.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
User Request URL-/old-page/app/dashboard/about/old-page/sub/app/new-page/old-page
Redirect Rule Match-YesNoNoNoNoNoYes
Rewrite Rule Match-NoYesNoNoYesNoNo
Final Response-Redirect to /new-pageServe /index.html content with URL /app/dashboardServe original /about contentServe original /old-page/sub contentServe /index.html content with URL /appServe original /new-page contentRedirect to /new-page
Key Moments - 3 Insights
Why does a redirect stop further processing of rewrite rules?
Because redirect sends a response back to the browser immediately (see execution_table step 1), so rewrite rules are not checked after a redirect match.
If a URL matches both redirect and rewrite rules, which one applies?
Redirect rules have priority and apply first (see step 1 and 7). Rewrite rules only apply if no redirect matches.
Does a rewrite change the URL shown in the browser?
No, rewrite serves different content internally without changing the URL (see step 2 and 5). The browser URL stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the final response at step 3 when the user requests '/about'?
ARedirect to /new-page
BServe /index.html content
CServe original /about content
DServe original /old-page/sub content
💡 Hint
Check the 'Final Response' column in row for step 3.
At which step does the redirect rule match the URL '/old-page'?
AStep 2
BStep 1
CStep 5
DStep 4
💡 Hint
Look at the 'Redirect Rule Match?' column for '/old-page' requests.
If the redirect rule for '/old-page' was removed, what would be the final response at step 1?
AServe original /old-page content
BRedirect to /new-page
CServe /index.html content
DServe original /about content
💡 Hint
Without redirect match, rewrite rules are checked; '/old-page' does not match rewrite rules in the table.
Concept Snapshot
Redirect and rewrite rules in Firebase:
- Redirects send a browser response to change URL.
- Rewrites serve different content without changing URL.
- Redirects checked first; if none match, rewrites apply.
- If no rules match, original content is served.
- Use redirects for URL changes, rewrites for SPA routing.
Full Transcript
When a user requests a URL, Firebase first checks if any redirect rule matches. If yes, it sends a redirect response to the browser, stopping further checks. If no redirect matches, it checks rewrite rules to serve different content internally without changing the URL shown. If no rewrite matches, the original content is served. Redirects have priority over rewrites. Rewrites are useful for single-page apps to serve index.html for many URLs. Redirects change the browser URL, rewrites do not.