DEV Community

Charlotte Towell
Charlotte Towell

Posted on

Using GCP Load Balancer to Handle 301 Redirects to Other Domains

Some of the main use cases of using a load balancer include setting up a static IP for use with a custom domain, or by dispersing traffic across multiple backend services.

A possibly less common use-case however is to use the balancer to handle 301 redirects in cases where your web application has undergone a domain migration. Here's how I set this up:

Domain Redirection using Load Balancer

Configure DNS Zones > Recordsets

In the case of a domain migration, you must route the old domain(s) to a load balancer. I choose to use the same load balancer as used for my backend service to centralise the logic, else you may need a 'dummy' backend that is not reached.

Routing Rules

Assuming you already have an application load balancer set up in Google (if not, see here), we can jump straight into editing the routing rules of the load balancer

Keep your existing routing rules, including default, to route to your desired backend(s). We will be clicking Add host and path rule, specifically for the old domain.

For the Hosts, add in your old domain(s).
For path matcher, you can copy & paste the following template:

defaultService: projects/<project-id>/global/backendServices/<backend-service-id>
name: matcher1
routeRules:
- urlRedirect:
    httpsRedirect: true
    stripQuery: true
    hostRedirect: newdomain.com
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  matchRules:
  - prefixMatch: /auth/ //only do for oldomain/auth/*
  priority: 1
- urlRedirect:
    httpsRedirect: true
    stripQuery: true
    hostRedirect: newdomain.com/login
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  matchRules:
  - prefixMatch: /util/login //only do for oldomain/util/login/
  priority: 2
- urlRedirect:
    httpsRedirect: true
    stripQuery: true
    hostRedirect: newdomain.com
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  matchRules:
  - prefixMatch: /util/ //only do for olddomain/util/*
  priority: 3
- urlRedirect:
    httpsRedirect: true
    stripQuery: true
    hostRedirect: anotherdomain.com
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  matchRules:
  - prefixMatch: / //everything 
  priority: 4
Enter fullscreen mode Exit fullscreen mode

Important Points:

  • Each rule (path matcher) must have a unique priority. Rules compute in cascading order, therefore you must put more specific matches first.
  • The MOVED_PERMANENTLY_DEFAULT code is what sets our 301 response
  • You can redirect to other domains & websites that are not the load balancer backend service, eg otherdomain.com
  • After the redirect to newdomain, it will then use your existing routing rules (if existing) to do any further routing before reaching the backend service(s)

At this point, you can save & test and should now successfully see the load balancer redirecting traffic from your old domain to new one based on whichever rules you have set!

Top comments (0)