Varnish/Fastly - Georedirecting All Site URLs to One Page Except for the Store Locator

50 Views Asked by At

I am attempting to redirect all traffic from certain countries to one centralized page (fall-2023-collection) except for one other page (store-locator). I have the redirection working in the sense that all pages redirect to fall-2023-collection, including store-locator. I have tried multiple conditional combinations with if/elseif/else but I have not had any luck getting the store-locator to load as expected.

This is what I have for the recv event;

if (req.http.host ~ "site\.url" && req.url != "/fall-2023-collection" && req.url != "/store-locator"  && req.url !~ "\.(jpe?g|gif|png|webp|css|js|json|svg|woff|woff2|html|)$" && table.lookup(georedirect, client.geo.country_code)) {
  error 618 "redirect";
}
elseif (req.http.host ~ "site\.url" && req.url == "/store-locator" && table.lookup(georedirect, client.geo.country_code)) {
  error 617 "redirect_loc";
}
else {
  return (pass);
}

And this is what I have for the error event;

if (obj.status == 618 && obj.response == "redirect") {
  set obj.status = 308;
  set obj.http.Location = "https://" + req.http.host + "/fall-2023-collection";
  return (deliver);
}

if (obj.status == 617 && obj.response == "redirect_loc") {
  set obj.status = 308;
  set obj.http.Location = "https://" + req.http.host + "/store-locator";
  return (deliver);
}

georedirect is just a table with the two letter country codes so it's not really relevant. That part works as expected. I verified that using a VPN so that's not where I'm having my main issue.

1

There are 1 best solutions below

1
Integralist On

A great tool to help you debug issues like this is Fastly Fiddle:
https://fiddle.fastly.dev/

I used it to produce a working example:
https://fiddle.fastly.dev/fiddle/be5f942f

In the linked fiddle I issue five requests:

  1. /anything/the-intended-destination?beep=boop
  2. /anything/dont-redirect-this-either?=fizz=buzz
  3. /anything/foo
  4. /anything/bar
  5. /anything/baz

The first request should just reach its destination.

The second request should just reach its destination.

The third, fourth and fifth requests should be redirected to /anything/the-intended-destination.

I didn't implement the table lookup as you mentioned this was working for you, and so the main change I made was a switch from req.url to req.url.path as the latter doesn't include query string params.

Any query string params on the incoming request would cause your check (which used req.url) to fail.

If you have any future questions or issues regarding Fastly, feel free to reach out to any of the following...

NOTE: The list above is in descending order of importance as far as official Fastly support (i.e. you'll get the least amount of eyes/feedback when posting to Stack Overflow). So I recommend asking questions either in the official community forum, or better still, emailing Fastly Support.