How to rewrite and set rootdir


(eric) #1

Hello, I want to rewrite an url like mydomain.com/path to mydomain.com/inner and set rootdir for this url.
This can be done in nginx:

   location /path {
        rewrite ^/path(.*)$ /inner$1 break;
        root /home/user/inner;
    }

But I can’t figure out how to achieve this in caddy, is this possible?


(Matthew Fay) #2

You won’t be able to rewrite and have a separate root for the rewritten URI, but you could do it with a redirect instead.

The other option is to structure your file system appropriately so that the inner folder is directly beneath the site root.


(Michael Munson) #3

He might be able to accomplish something via a complicated combination of rewrites and http.filter plugin use, but it wouldn’t be performant.


(Matthew Fay) #4

I’m not sure you can. If my understanding is correct, the root directive sets the web root directly, so the last instance of it within a site definition “wins” (the previous declarations are discarded). You just can’t have two separate web roots in one site, so the only way to go about it would be to have one site for example.com, with its own root, and one site at example.com/path, with another web root.


(Michael Munson) #5

Yeah, maybe if the second root was entirely contained within the first, but that doesn’t make sense. Caddy lacks the equivalent of a location directive because of how complicated it would be to integrate with things like Hugo plugin being able to create an entire static site on server start, etc. It’d be nice, though – and ultimately I think necessary.