Relative paths in html files do not work

1. The problem I’m having:

I want to serve static html files. Those files are using relative paths “./abc” to other assets. This does not work because the base path is lost.

So, I can access host:86/munin/index.html.

But in that html file, it references other files and directories with “./otherdir/otherfile”.

With Caddy, the base path “/munin” is lost and the browser want to go to host:86/otherdir/otherfile.

This worked fine with Apache.

2. Error messages and/or full log output:

No errors.

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

Docker compose.

c. Service/unit/compose file:

services:
  caddy:
    build:
      context: .
      dockerfile: Dockerfile-caddy
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
    ports:
      - "86:80"
      - "446:443"
      - "446:443/udp"
    networks:
      - caddy
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/site:/srv
      - $PWD/data:/data
      - $PWD/config:/config
      - type: bind
        source: /var/www/localhost/htdocs/munin
        target: /munin

d. My complete Caddy config:

{
        auto_https off
        order webdav before file_server
}

:80 {
        handle_path /munin* {
                root * /munin
                file_server
        }
}

I am using handle_path because I am serving also other parts with other features (php, webdav).

Thanks!

You need handle_path /munin/*.

(This isn’t a Caddy issue, this is just how relative links work in HTML.)

2 Likes

Very cool, thank you for the quick response, this works.

I have seen this syntax in examples but did not think that this would fix my issue.

As expected, then host:86/munin did not work anymore.

But I was able to fix that with

redir /munin /munin/

outside the handle_path directives.

1 Like