Rewrite actually redirecting

Caddy version 0.9.1

example.com:80 {
  root /etc/source
  gzip
  log stdout
  errors stdout
  tls off
  git {
    repo     git@dev.example.com/test/test.git
    branch   master
    path     test-x
    key      /etc/secret/.deploykey
    interval 2300000
  }
  rewrite {
    if {path} has test
    to /test-x{uri} 
  }
}

This does an internal rewrite as one would think when going to http://example.com/test/, but does additionally do a non internal redirect when going to http://example.com/test.

http://example.com/test/ → serves files from /text-x/ as expected
http://example.com/test → redirects to http://example.com/test-x/test/

Is this intentional? Did I somehow misconfigure?

Thanks for any hints.

The redirect is happening because the canonical URL for a directory has a trailing forward slash, then the rewrite is taking effect. So rewrite isn’t redirecting, it’s the internal file server that is redirecting to a canonical URL.

To better understand the reasoning behind some aspects I did some tests and this came up:

Caddyfile:

    :8080/example.com {
      root /etc/source/example.com/
      gzip
      log stdout
      errors stdout
    }
    :8080 {
      redir / https://main.example.com 302
    }

Some requests and the results:
:8080/file → file is served
:8080/example.com/ → html is served
:8080/example.com → redirect occurs
:8080/example.com/dir → redirect occurs
:8080/example.com/dir/ → html is served
:8080/example.com/dir/index.html → html is served
:8080/example.com/dir/index.html/ → redirect occurs

Shouldn’t at least some of the redirects be handled as error within the first part of the caddyfile? It seems the canonical redirect is not yet perfectly reasonable for me or at least not how it affects the provided caddyfile.
As a sidnote non existing files produce 404 errors within the first caddyfile part.

Thanks for anyone taking the time to clarify canonical redirects and their effects here. Perhaps some reasoning, why this makes sense would be much appreciated.

Cheers

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