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

(martpie) #1


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.



what happens: -> index.html is served [OK] -> something.html is served [OK] -> 301 -> /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: {
  root /somewhere/directory
  ext .html

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

(Matthew Fay) #2

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

(martpie) #3

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?

(martpie) #4

Well it seems there’s not much to do here: this redirect seems hardcoded and there is no way I found to avoid it:

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

(Matthew Fay) #5

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.