· Updated · 5 min read ·
tutorials google-cloud domain-forwarding redirects

Redirect Domain Using Google Cloud Functions

Ekke Uustalu
Ekke Uustalu · Founder
Google Cloud Functions domain redirect tutorial

TL;DR: Google Cloud Functions can handle domain redirects with a Node.js function, custom environment variables, and Cloud Run domain mapping. It’s free for up to 2M requests/month but takes 20+ minutes to configure. For a no-code alternative, Domain-Forward.com does the same thing in 5 minutes.


Introduction

This tutorial covers domain redirection using Google Cloud Functions — a serverless approach that’s pay-per-use with a generous free tier: 2 million monthly invocations at no cost.

By the end, you’ll have a deployed Cloud Function that redirects traffic to your target URL with configurable path and query parameter forwarding.

Prerequisites

Before we get started, ensure that you have the following:

  • A Google Cloud account.
  • Basic understanding of Node.js and JavaScript.

Steps to Redirect Traffic Using Google Cloud Functions

Step 1: Setting Up Your Google Cloud Function

Create a new Cloud Function in the Google Cloud Console. Navigate to Cloud Functions from the sidebar.

Finding the cloud functions in the Google Cloud Panel

Enable the APIs that are required.

Enabling the required APIs for Cloud Functions

Click ‘Create Function’.

Creating the Cloud Function

Configure the function:

  • Provide a name
  • Select 2nd gen as the environment
  • Select us-central1 as the location
  • Choose HTTPS as the trigger
  • Check Allow unauthenticated invocations

Step 2: Environment Variables Configuration

Set these environment variables:

  • REDIRECT_CODE: This is the HTTP status code that will be sent back to the client to indicate the redirection. Commonly used values are ‘301’ (Moved Permanently) or ‘302’ (Found).
  • TARGET_URL: This is the URL where you want to redirect the traffic.
  • FORWARD_PATH: If set to ‘true’, the source URL path will be appended to the TARGET_URL. If ‘false’, only the root TARGET_URL will be used.
  • FORWARD_QUERY: If set to ‘true’, any query parameters in the original URL will be forwarded to the TARGET_URL.

Example: redirect https://old-domain.com to https://google.com with path (/contact) and query parameters (?utm_source=xyz) forwarded.

Assigning variables to the cloud function

Step 3: Write Your Cloud Function

Click ‘Next’ to open the source code section. Set the runtime to Node.js and paste this code into index.js:

const functions = require('@google-cloud/functions-framework');
const url = require('url');

functions.http('helloHttp', (req, res) => {
  const targetUrl = url.parse(process.env.TARGET_URL, true);
  const redirectCode = parseInt(process.env.REDIRECT_CODE);
  const shouldForwardPath = process.env.FORWARD_PATH === 'true';
  const shouldForwardQuery = process.env.FORWARD_QUERY === 'true';

  if (shouldForwardPath) {
      targetUrl.pathname += req.path;
  }

  if (shouldForwardQuery && Object.keys(req.query).length > 0) {
      targetUrl.query = {...req.query, ...targetUrl.query};
  }

  // remove old 'search' property which can conflict with 'query'
  targetUrl.search = null;

  const redirectUrl = url.format(targetUrl);

  res.redirect(redirectCode, redirectUrl);
});

The function reads the environment variables, builds the redirect URL (appending path and query parameters as configured), and returns the redirect response.

It also merges incoming query parameters with any existing parameters in the targetUrl.

Pasting the code in the cloud function creation UI

Step 4: Deploy Your Function

Click ‘Deploy’ to make the function live. Google Cloud provides a URL for testing once deployment completes — use it to verify your configuration.

Next, map your actual domain to this function.

Step 5: Mapping a Custom Domain to Your Cloud Function

Custom domain mapping requires Google Cloud Run. Navigate to Cloud Run using the search bar.

Finding Cloud Run using search

From the top menu, select ‘Manage custom domains’ and click ‘Add mapping’.

List of existing Cloud Run services

Select the Cloud Function you created. Identify it by the name you gave it in Step 1.

Creating a custom domain mapping for the cloud function

You need to verify domain ownership via Google Search Console. Follow this verification tutorial if you haven’t done this before.

Custom domain mapping is pending

Once verified, return to Google Cloud Platform and continue with the domain mapping.

Domain ownership verified in Google Search Console

After validation, click ‘Continue’ and configure the A and AAAA records at your domain registrar as shown. Map both the root domain and www subdomain to the same Cloud Function.

Finalising the custom domain mapping

Once DNS records propagate (up to 24 hours), your custom domain will serve the redirect function over HTTPS.

Conclusion

Your Cloud Function is now handling domain redirects. Adjust the environment variables anytime to change the redirect target, status code, or path/query forwarding behavior.

How can I do it without writing code?

If you are looking for an easy way to redirect a domain without configuring cloud infrastructure, use Domain-Forward.com. You can create your first redirect using the Getting Started guide. No code, no cloud setup, and automatic HTTPS included.

Looking for other redirect methods? Check out our guides for AWS Route53 and Google Cloud Run, or read our overview of all domain redirect options. You can verify your redirect with our redirect tester tool.

Frequently Asked Questions

How do I redirect a domain using Google Cloud Functions?
Create a Cloud Function with a Node.js redirect handler, configure environment variables for the target URL and redirect type (301/302), deploy it, then map your custom domain via Google Cloud Run. The function handles all redirect logic.
How much does Google Cloud Functions cost for domain redirects?
Google Cloud Functions has a free tier of 2 million monthly invocations. For most domain redirect use cases, this is more than enough. Beyond the free tier, pricing is per-invocation.
Can I preserve URL paths when redirecting with Cloud Functions?
Yes. Set the FORWARD_PATH environment variable to 'true' and the function will append the original path to the target URL. Similarly, FORWARD_QUERY preserves query parameters.
Is there an easier way to redirect a domain than Google Cloud Functions?
Yes. Domain-Forward.com provides domain redirects with automatic HTTPS, 301 redirects, and analytics — no code or cloud configuration needed. Free plan available for up to 5 domains.

Ready to simplify your redirects?

Start forwarding domains in minutes. Free plan available.

Get Started Free