Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

What is the difference between routeRules and pathRules in a URL map?

What is the difference between routeRules and pathRules in a URL map?

I can see the reference docs for pathRules:

https://cloud.google.com/sdk/gcloud/reference/compute/url-maps/add-path-matcher

Also, the following doc:

 
 
But i cannot understand what routeRules are?
1 5 1,113
5 REPLIES 5

Can someone please help.

You can use path rules to direct requests to `/blog` to one backend service and requests to `/shop` to another. For example, a path rule might look like this:

```yaml
pathRules:
- paths: ["/blog/*"]
service: blog-backend
- paths: ["/shop/*"]
service: shop-backend
```

If you want to route requests based on more than just the path, like a specific header or query parameter, you would use route rules. For example, you could route requests with a specific header to a different backend:

```yaml
routeRules:
- matchRules:
- prefixMatch: "/shop"
headerMatches:
- headerName: "X-User-Type"
exactMatch: "premium"
service: premium-shop-backend
- matchRules:
- prefixMatch: "/shop"
service: regular-shop-backend
```

In this example, requests to `/shop` with the header `X-User-Type: premium` go to the `premium-shop-backend`, while all other `/shop` requests go to the `regular-shop-backend`.

ah okay @yrhsk your explanation is very helpful.

Thank you!

Hi @mountaincode2,

Welcome to Google Cloud Community!

You’ll find the answer in the Traffic management overview for internal Application Load Balancers. According to the documentation:

“Path rules (pathRules) specify one or more URL paths, such as / or /video. Path rules are generally intended for the type of simple host and path-based routing”

“Route rule (routeRules) matches information in an incoming request and makes a routing decision based on the match.”

For a more detailed explanation of what are the differences between the two, please see the documentations below:

Hope this helps.

The following is yet another difference between path rules and route rules as stated in the following doc:

 

When you use path rules in a path matcher, you cannot create more than one path rule for the same path. For example, requests for /videos/hd cannot be directed to more than one backend service or backend bucket. Backend services can have backend instance groups or backend network endpoint groups (NEGs) in different zones and regions, and you can create backend buckets that use Multi-Regional Storage classes.

To direct traffic for a unique URL to multiple services, you can use route rules instead of path rules. If you configure the path matcher with route rules for header or parameter matches, a unique URL can be directed to more than one backend service or bucket, based on the contents of headers or query parameters on the URL.