- Unsupervised Learning
- Posts
- Adding a Trailing Slash to Directories Using Varnish
Adding a Trailing Slash to Directories Using Varnish
![wyvern-programming-languages-in-one](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/1b1f61de-2a34-4bea-a967-68b327c51f28/wyvern-programming-languages-in-one.jpg)
My web configuration makes use of Varnish as a front-end, and I noticed a bug recently where asking for “/study” was failing, while asking for “/study/” would succeed.
But when I moved Varnish out of the way, Nginx handled this without issue.
So I set out to figure out a recipe for adding the trailing slash within Varnish. The following solution works for me, but if you know a better way do let me know.
# If the requested URL contains $directory and doesn’t contain index.php, add a trailing slash to the end.
sub vcl_recv { if ((req.url ~ "/directory" ) && (! (req.url ~ "index.php"))){ set req.url = req.url "/"; } }
::