Serve files from directory to / and index.html at the same time

Sooo I’m trying to
Serve both root directory and images/other files that are in /app/static/
like /app/static/img.png and literally can’t find any way to do it, tried many things with rewrite, tried looking up google, no luck. {
     root ./app

anyone has any idea? I’d be grateful, spent too much time on this already…

I’m not sure I fully understand - is /app/static the root directory as well as the directory containing the files?

Have you tried just root /app/static on its own?

no /app/static isn’t root directory, /app is
and like it has
/app/ {

and index.html is in app which is root, but i also want to serve files from static which is in app too

Sometimes images and other static content are served on a cookieless subdomain.

Could you do that with your static files?

well I’d prefer to have no subdomain.
i want to return normal website and like return the file I’ll upload ;s
It would be easier to setup on subdomain for sure but im trying to find a solution to do without it

An easy way would be to move static files inside your main root folder.

You could do a rewrite to hide the real location of the file if you want to do that:

Well that would be really weird since there are thousands of them, im trying to make a file hosting so i want it all sorted. It was possible in other webservers but i moved to caddy since i want to host multiple domains and its easiest here

Could you please share your exact config on other webservers?

Maybe there’s a way to reproduce it.

Well, they don’t really have a config like caddy.
Ive been using echo before and simply made a few functions in golang
e.File("/", “index.html”)
e.GET("/:id", showImage)
e.POST("/upload", Upload)

Probably i could make proxy and stuff but whats the point to use caddy then. Im pretty sure its possible to do with caddy rewrite, i just don’t know how

Actually, why? You’ll have thousands of files in a directory somewhere, so why not have them in the same one as contains index.html?

because its a bad practice. I don’t want to put everything in one directory with files that are being uploaded. It’s better to not do it at all than do it that way.

You can use this to try files that may exist in a subdirectory of your web root prior to trying files in the web root itself: {
  root /app
  rewrite {
    to /static{path} {path}

This will, for a request to /foo.jpg, first attempt to serve the file /app/static/foo.jpg before falling back and trying /app/foo.jpg.

Sadly doesn’t serve index.html from root on main page, i’m getting 404 not found.
Seem to work fine with serving files from static but i need both ;s


rewrite {
  to /static{path} {path}/ {path}

Which should also check for instances of {path} as a directory to be served via index file.

welp, no luck.
Doesn’t see that index.html in root, static serves fine.

Hmm… Could you remove the rewrite entirely and confirm that the index file is served correctly?

Yes i confirm its served correctly without rewrite.
With rewrite my config looks like: {
        root ./app
        rewrite {
                to /static{path} {path}/ {path}

That’s pretty weird… not the result I expected - when I sit down next in front of one of my computers with a bash terminal I’ll fiddle around and try and get some results.

This definitely should be doable…

I played around, and found this did what my understanding of the requirement was: {
	root ..\dummysite
	rewrite {
		ext !/
		to /static{path} {path}

The “ext !/” prevents it rewriting directories, and so lets it find index.html in the root.


I then considered the possibility of subdirectories in /static, and came up with:

	rewrite {
		if {path} not /
		to /static{path}/ /static{path} {path}/ {path}

Note that even with all those options in the “to” directive, the “if {path} not /” remained necessary. But this now finds index files in subdirectories of static (without “static” being specified), as well as the one in the root.

Is there something wrong with the handling of the path “/” in rewrite?


Great! Seems like it does what I wanted, though prevents from viewing source of the page by redirecting to so its not perfect but yeah root directory works, static works, does its job.

I also came up with idea of just serving both index.html and correct file from static by using proxy and some other framework.

But yeah seems like its solved, thank you! :slight_smile: