I could not find anything related to that, maybe because of the wrong keywords.
My problem is the following: if I have an index.html in a subdirectory, Caddy will redirect a request with a trailing slash.
files:
/index.html
/something.html
/blog/index.html
what happens:
https://domain.com -> index.html is served [OK]
https://domain.com/something -> something.html is served [OK]
https://domain.com/blog -> 301 https://domain.com/blog/ -> /blog/index.html is served [NOT OK]
=> Here'd I'd like no 301, and no trailing slash to be appended
Here is my config:
domain.com {
root /somewhere/directory
ext .html
gzip
# basic caching
expires {
match \.(?:ico|css|js|gif|jpe?g|png)$ 356d
}
# force http to https
redir 301 {
if {>X-Forwarded-Proto} is http
/ https://{host}{uri}
}
}
I made a typo in my post, I meant “if possible without renaming the files”. I am using Nuxt as a framework and it requires the file structure I’ve shown for routing.
I tried to implement what was specified here, but I get a redirection problem (an infinite redirection I guess)
rewrite {
if {path} not /
if {path} ends_with /
r ^/(.*)/$
to /{1}
}
redir 301 {
if {path} not /
if {path} ends_with /
/ {rewrite_uri}
}
Any way to prevent this 301 redirection? or to make the config above work? Or is it a hardcoded “feature” of Caddy?
Yeah, unfortunately in your case, this is an opinionated stance of the Caddy static fileserver to ensure the canonical method of requesting a directory index is adhered to.
So after some research, Nuxt.js has actually an option that solves this exact problem. For a future reference, use generate.subFolders and set it to false