Is this condition correct?
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
-
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 vclmiss and vclpass. 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
Please sign in to leave a comment.
Comments
3 comments