Bird
Raised Fist0
Firebasecloud~5 mins

Redirect and rewrite rules in Firebase - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to send visitors from one web address to another or change how URLs work without changing the visible address. Redirect and rewrite rules help you do this easily on your Firebase-hosted website.
When you want to send users from an old page URL to a new page URL automatically.
When you want to serve a single page app but keep clean URLs without .html extensions.
When you want to hide the real location of your files by rewriting URLs internally.
When you want to fix broken links by redirecting them to working pages.
When you want to change URL paths for SEO or user-friendly addresses without changing your files.
Config File - firebase.json
firebase.json
{
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "/app/**",
        "destination": "/index.html"
      }
    ],
    "redirects": [
      {
        "source": "/old-page",
        "destination": "/new-page",
        "type": 301
      }
    ]
  }
}

hosting.public: The folder with your website files.

rewrites: Changes URLs internally, like sending all /app/* requests to index.html for single page apps.

redirects: Sends visitors from one URL to another with a status code (301 means permanent redirect).

Commands
Uploads your firebase.json and website files to Firebase hosting, applying your redirect and rewrite rules.
Terminal
firebase deploy --only hosting
Expected OutputExpected
=== Deploying to 'your-project-id'... i deploying hosting i hosting[your-project-id]: beginning deploy... āœ” hosting[your-project-id]: deployed successfully āœ” Deploy complete!
→
--only hosting - Deploys only the hosting part, skipping other Firebase services.
Checks the HTTP headers for the old page URL to verify it redirects to the new page.
Terminal
curl -I https://your-project-id.web.app/old-page
Expected OutputExpected
HTTP/2 301 location: https://your-project-id.web.app/new-page cache-control: public, max-age=3600 content-length: 0
Checks that the /app/dashboard URL is rewritten internally to serve index.html without changing the URL in the browser.
Terminal
curl -I https://your-project-id.web.app/app/dashboard
Expected OutputExpected
HTTP/2 200 content-type: text/html; charset=utf-8 cache-control: public, max-age=3600
Key Concept

If you remember nothing else from this pattern, remember: redirects send users to a new URL visibly, while rewrites serve content from a different location without changing the URL shown.

Common Mistakes
Using rewrites when you want users to see the new URL.
Rewrites keep the original URL in the browser, so users don't know they are seeing different content.
Use redirects for sending users to a new URL that shows in the browser address bar.
Not specifying the correct HTTP status code for redirects.
Without a proper status code like 301 or 302, browsers and search engines may not handle the redirect correctly.
Always specify 301 for permanent or 302 for temporary redirects in your firebase.json.
Forgetting to deploy after changing firebase.json.
Changes to redirect and rewrite rules only take effect after deployment.
Run 'firebase deploy --only hosting' after editing firebase.json.
Summary
Create redirect and rewrite rules in firebase.json under hosting section.
Use redirects to send users to new URLs with visible address changes.
Use rewrites to serve content from different files without changing the URL.
Deploy changes with 'firebase deploy --only hosting' to apply rules.
Verify redirects and rewrites using curl or browser tests.

Practice

(1/5)
1. What is the main difference between a redirect and a rewrite in Firebase Hosting?
easy
A. Redirect changes the browser URL, rewrite does not change the URL shown.
B. Rewrite changes the browser URL, redirect does not change the URL shown.
C. Both redirect and rewrite always change the browser URL.
D. Neither redirect nor rewrite affect the browser URL.

Solution

  1. 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.
  2. Step 2: Understand rewrite behavior

    A rewrite serves different content but keeps the original URL visible in the browser address bar.
  3. Final Answer:

    Redirect changes the browser URL, rewrite does not change the URL shown. -> Option A
  4. Quick Check:

    Redirect updates URL, rewrite keeps URL same [OK]
Hint: Redirect changes URL, rewrite hides content change [OK]
Common Mistakes:
  • Confusing redirect with rewrite behavior
  • Thinking rewrite changes browser URL
  • Assuming redirect does not update URL
2. Which of the following is the correct Firebase Hosting syntax to redirect from /old-page to /new-page?
easy
A. "redirect": [{"source": "/old-page", "destination": "/new-page"}]
B. "rewrites": [{"source": "/old-page", "destination": "/new-page"}]
C. "rewrites": [{"source": "/old-page", "type": 301, "destination": "/new-page"}]
D. "redirects": [{"source": "/old-page", "destination": "/new-page", "type": 301}]

Solution

  1. Step 1: Identify redirect syntax

    Redirects require a source, destination, and a type (like 301 for permanent redirect).
  2. 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.
  3. Final Answer:

    "redirects": [{"source": "/old-page", "destination": "/new-page", "type": 301}] -> Option D
  4. Quick Check:

    Redirect needs type code like 301 [OK]
Hint: Redirects need a type code (301 or 302) [OK]
Common Mistakes:
  • Omitting the redirect type code
  • Using rewrites instead of redirects for URL change
  • Placing type in rewrites block
3. Given this Firebase Hosting config snippet:
{
  "rewrites": [
    {"source": "/app/**", "destination": "/index.html"}
  ]
}

What happens when a user visits /app/profile?
medium
A. The browser URL stays /app/profile but serves content from /index.html.
B. The browser URL changes to /index.html and shows that page.
C. The browser URL changes to /app/index.html and shows that page.
D. The browser returns a 404 error because /app/profile does not exist.

Solution

  1. Step 1: Understand rewrite with wildcard

    The rewrite rule matches any URL starting with /app/ and serves /index.html content without changing the URL.
  2. Step 2: Apply to /app/profile

    Visiting /app/profile triggers the rewrite, so content from /index.html is served but URL remains /app/profile.
  3. Final Answer:

    The browser URL stays /app/profile but serves content from /index.html. -> Option A
  4. Quick Check:

    Rewrite serves content without URL change [OK]
Hint: Rewrite serves content but keeps URL same [OK]
Common Mistakes:
  • Thinking rewrite changes the URL
  • Expecting 404 error for unmatched files
  • Assuming redirect happens instead of rewrite
4. You wrote this Firebase Hosting config:
{
  "redirects": [
    {"source": "/home", "destination": "/index.html"}
  ]
}

But the redirect does not work as expected. What is the problem?
medium
A. Using redirects instead of rewrites for serving index.html.
B. The source path should be /home/** to match all subpaths.
C. Missing the redirect type (301 or 302) in the redirect rule.
D. The destination should be a full URL, not a relative path.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Missing the redirect type (301 or 302) in the redirect rule. -> Option C
  4. Quick Check:

    Redirect requires type code [OK]
Hint: Redirects need a type code (301 or 302) [OK]
Common Mistakes:
  • Using redirects instead of rewrites to preserve URL
  • Confusing redirects with rewrites
  • Using relative paths incorrectly
5. You want to support a single-page app on Firebase Hosting where all URLs under /app/ show index.html content without changing the URL, but also redirect /old-app permanently to /app/. Which config snippet correctly achieves this?
hard
A. { "rewrites": [ {"source": "/old-app", "destination": "/app/"}, {"source": "/app/**", "destination": "/index.html"} ] }
B. { "redirects": [ {"source": "/old-app", "destination": "/app/", "type": 301} ], "rewrites": [ {"source": "/app/**", "destination": "/index.html"} ] }
C. { "redirects": [ {"source": "/old-app/**", "destination": "/app/", "type": 302} ], "rewrites": [ {"source": "/app/**", "destination": "/index.html"} ] }
D. { "redirects": [ {"source": "/old-app", "destination": "/index.html", "type": 301} ], "rewrites": [ {"source": "/app/**", "destination": "/app/"} ] }

Solution

  1. Step 1: Redirect /old-app to /app/ permanently

    Use a redirect with type 301 from /old-app to /app/ to update browser URL.
  2. 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.
  3. 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.
  4. Final Answer:

    { "redirects": [ {"source": "/old-app", "destination": "/app/", "type": 301} ], "rewrites": [ {"source": "/app/**", "destination": "/index.html"} ] } -> Option B
  5. Quick Check:

    Redirect updates URL, rewrite serves SPA content [OK]
Hint: Redirect for URL change, rewrite for SPA content [OK]
Common Mistakes:
  • Using rewrite instead of redirect for URL change
  • Missing redirect type or using wrong type
  • Rewriting to wrong destination path
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard