Is this condition correct?


#1

Hi,

Can someone confirm this condition will remove trailing slashes from our URL’s?

(req.url ~ “/”) set req.http.host = regsuball(req.url, “”);

Kind Regards,
Keiran


#2

Hi Keiran

If I understand correctly you’d like to remove the trailing slashes from the request to your origin, is this right?
If so, one approach is edit the req.url as soon as Fastly receives it, in other words, in the vcl_recv subroutine.

Particularly, for simple substitutions I prefer to make a more readable code than saving lines with regsuball.

declare local var.qs STRING;
if (req.url.path ~ "(.*)/$") {
  set var.qs = req.url.qs;
  set req.url = re.group.1;
  if ( var.qs != "" ) {
    set req.url = req.url + "?" + var.qs;
  }
}

The above condition tests against req.url.path instead of req.url because the trailing “/” will be detected even if you have query strings. The expression uses “$” to anchor the trailing “/” at the end of of the path.

Here is the fiddle that demonstrates this setup: https://fiddle.fastlydemo.net/fiddle/87beb74f. Fiddle is an experimental tool used to watch what’s going on in each step of VCL processing at Fastly.

Notice you maybe don’t want to modify the req.url variable… maybe you need to know exactly what the user sent you in a future stage of your VCL code. If this is the case, consider modifying bereq.url instead in both vcl_miss and vcl_pass. These are the VCL subroutines used to contact the origin.

In other words, if you modify req.url, both req.url and bereq.url will be changed. If you modify bereq.url, the original req.url will be preserved.

Regards,

Fernando


#3

Hi Fernando,

Thank you for your message. I’m afraid we have now stopped using Fastly due to the large number of issues we encountered. I would be grateful if I can be removed from this topic trail.

Kind Regards,

Keiran


#4

Hi Keiran, @mastershoe,

Sorry to hear that. I believe you need to edit your Preferences, and disable all Notifications.
Kindest,

Fernando