Caddy redirecting and appending trailing slash with subdirectory/index.html?

Hello,

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 guess it’s a Caddy default but I would like Domain Blog | Domain, Website, and Ecommerce Tips for Small Businesses to be directly accessible, with no trailing slash or redirection, it it doable?

If possible without renaming /blog/index.html to /blog.html :slight_smile:

You can rename your blog page to /blog.html and use ext .html, which will serve the blog HTML without needing the file extension.

https://caddyserver.com/docs/ext

Hello @Whitestrake,

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?

Well it seems there’s not much to do here: this redirect seems hardcoded and there is no way I found to avoid it: https://github.com/mholt/caddy/blob/master/caddyhttp/staticfiles/fileserver.go#L114

I commented on this issue to explain my usecase. Hopefully one day Caddy will change this behavior.

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 :tada:

2 Likes

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