/firebase-tutorials

How to redirect URLs in Firebase hosting?

Learn how to redirect URLs on Firebase Hosting step-by-step. Set up your project, configure firebase.json, deploy updates, and test your redirects efficiently.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to redirect URLs in Firebase hosting?

 

Step 1: Set Up Firebase Hosting

 

To redirect URLs using Firebase Hosting, you first need to have a Firebase project set up with hosting enabled.

  1. Create a Firebase Project:
  • Go to the Firebase Console.
  • Click on "Add project" and follow the prompts to create a new project.
  1. Enable Hosting:
  • After creating the project, navigate to the Hosting section.
  • Click on "Get Started" to enable Firebase Hosting.

 

Step 2: Install Firebase CLI

 

The Firebase CLI (Command Line Interface) allows you to interact with Firebase Hosting from your local machine.

  • Install Node.js:
    Make sure you have Node.js installed on your machine. You can download it from nodejs.org.

  • Install Firebase CLI:
    Open your terminal or command prompt and run the following command:

    npm install -g firebase-tools

 

Step 3: Initialize Firebase in Your Project

 

You need to initialize Firebase in your project directory.

  • Open your terminal in your project directory and run:

    firebase init
  • Select Hosting:
    Move with the arrow keys to select "Hosting" and then press the space bar to choose it. Press Enter to confirm your selection.

  • Associate it with your Firebase project:
    Firebase CLI will ask you to select an existing project or create a new one. Select the one you created in Step 1.

  • Set up a public directory:
    You'll be asked to define a public directory. This is where your static files will live. Common choices include public or dist. You can type the name and press Enter.

  • Configure as a single-page app (if applicable):
    If you are using a Single Page Application like React, Angular, or Vue, type Y when asked "Configure as a single-page app (rewrite all urls to /index.html)?", otherwise type N.

 

Step 4: Configure URL Redirects

 

To define URL redirects, you need to add them to the firebase.json configuration file in your project root.

  • Open the firebase.json file:
    You should find the firebase.json file in your project root directory.

  • Add redirect rules under the "hosting" key:
    Here’s an example of how to set up a redirect:

    
    {
      "hosting": {
        // Other configurations...
        "redirects": [
          {
            "source": "/old-url",
            "destination": "/new-url",
            "type": 301
          }
        ]
      }
    }
    
  • Explanation:

    • source: The URL path you want to redirect from. It can include wildcards or regex patterns.
    • destination: The target URL path you want to redirect to.
    • type: The HTTP status code to use for the redirect. Use 301 for permanent redirects and 302 for temporary.

 

Step 5: Deploy Your Changes

 

To apply your changes, you need to deploy them to Firebase Hosting.

  • Run the deploy command:

    firebase deploy
  • Verify Deployment:
    Once completed, the terminal will output a hosting URL where you can verify your redirects.

 

Step 6: Test Your Redirects

 

To ensure your redirects are working correctly, visit the source URLs in your browser and verify that they redirect to the designated destination URLs.

  • Check HTTP status codes using browser developer tools or online tools to confirm that the correct HTTP status codes (e.g., 301, 302) are being used for redirects as specified.

 

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022