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.
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:
/anything/the-intended-destination?beep=boop/anything/dont-redirect-this-either?=fizz=buzz/anything/foo/anything/bar/anything/bazThe 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.urltoreq.url.pathas 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...