Serve same file from different paths

1. The problem I’m having:

How do I serve the same file under different paths? I have an index.html at my root. I would like it to be served from every path. So /a/b/c/d would serve index.html /c/d/e would serve index.html. /* would serve index.html. The reason I want to do this is because of how react routing works, although that’s not important. What should my caddyfile look like?

2. Caddyfile Config:

:8080 {

file_server

}

Howdy @aprogrammer, welcome to the Caddy community.

Have you had a look at try_files? That’s the directive you’ll need to use to rewrite to a web root index such as for an SPA.

Give the docs a read, and take a peek in particular at the very first example, which should give you a good idea of how to proceed.

1 Like

I found the below link to be more helpful I was trying to use try_files but I think one of things was I was supposed to be doing index.html instead of /index.html. /index.html doesn’t even make much sense. I also omitted the root. Since the location of the my index file was the same as my caddyfile.
Final Caddy File:

:8080 {

    handle /Protected/* {
        reverse_proxy http://example.com:8080
    }

    handle /Passthrough/* {
        reverse_proxy http://example.com:8080
    }

    handle {
		try_files {path} index.html
		file_server
	}
}

Common Caddyfile Patterns — Caddy Documentation (caddyserver.com)

1 Like

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