Issue with docs Caddyfile, causes pages to load incorrectly with a trailing /

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

a. System environment:

Windows 10, Powershell, development environment

b. Command:

caddy run

d. My complete Caddyfile or JSON config:

http://localhost:2015

root * src

file_server
templates
encode gzip

try_files {path}.html {path}

redir   /docs/json      /docs/json/
redir   /docs/modules   /docs/modules/
rewrite /docs/json/*    /docs/json/index.html
rewrite /docs/modules/* /docs/modules/index.html
rewrite /docs/*         /docs/index.html

reverse_proxy /api/* localhost:4444

3. The problem I’m having:

I am working on doing some basic cleanups in teh v2 docs, and noticed that if you give a url like
https://caddyserver.com/docs/caddyfile/matcher/ it gives a white screen,

This is also reproducible on the official docs web page

4. Error messages and/or full log output:

2020/07/23 18:39:15.870 ERROR  http.log.error  template: /docs/index.html:4:20: executing "/docs/index.html" at <include $markdownFilePath>: error calling include: open src\docs\markd
own\install\.md: The system cannot find the path specified.     {"request": {"method": "GET", "uri": "/docs/install/", "proto": "HTTP/1.1", "remote_addr": "[::1]:54320", "host": "localhost:2015", "headers": {"User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "Referer": ["http://localhost:2015/docs/"], "Upgrade-Insecure-Requests": 
["1"]}}, "duration": 0.0030252, "status": 500, "err_id": "tp3q5fbby", "err_trace": "templates.(*Templates).executeTemplate (templates.go:305)"}

5. What I already tried:

redir /docs/*/ /{path} which just infinately redirects to /docs//docs//<ad-infinitum>/install/

6. Links to relevant resources:

Could just strip the suffix.

Looks like there’s literally only two pages with trailing slashes, /docs/json/ and /docs/modules/.

Give them a quick dodge and then remove any trailing slash:

@noslash not path /docs/json/ /docs/modules/
uri @noslash strip_suffix /

Should probably work pretty well.

nono, this is as in if a link is not done properly it and there is a trailing slash, like you meant to link /docs/install but put /docs/install/

Yes, that’s correct. With the trailing slash present, a lot of those requests have a bad result (blank page).

If you rewrite to strip the suffix /, Caddy would treat a request for /docs/install/ as though it were a request for /docs/install instead. Which should be correct.

We don’t want to do this for paths /docs/json/ and /docs/modules/, though, because those two need the trailing slash. So we exclude those from the uri directive.

This transparently fixes the issue, no?

This topic was automatically closed after 30 days. New replies are no longer allowed.