Rewrite to index.txt does not appear to work


(Sugarcube) #1

Rewriting to index.txt appears not to work.
Maybe this is a bug, maybe i’m doing something wrong.

I tried following: create folder with 2 files index.txt, test.txt with same content and launch caddy:

Caddyfile:

:2015 {
	tls off
	rewrite .* /index.txt
}

Curl says:

>>>>curl -v localhost:2015
* Rebuilt URL to: localhost:2015/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 2015 (#0)
> GET / HTTP/1.1
> Host: localhost:2015
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: /
< Server: Caddy
< Date: Wed, 27 Feb 2019 23:18:51 GMT
< Content-Length: 36
<
<a href="/">Moved Permanently</a>.

* Connection #0 to host localhost left intact

Replacing /index.txt with /test.txt works:

Caddyfile:
:2015 {
	tls off
	rewrite .* /test.txt
}
>>>>>curl -v localhost:2015
* Rebuilt URL to: localhost:2015/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 2015 (#0)
> GET / HTTP/1.1
> Host: localhost:2015
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 4
< Content-Type: text/plain; charset=utf-8
< Etag: "pnlxnq4"
< Last-Modified: Wed, 27 Feb 2019 23:09:26 GMT
< Server: Caddy
< Date: Wed, 27 Feb 2019 23:20:39 GMT
<
test* Connection #0 to host localhost left intact

(Matthew Fay) #2

Hi @sugarcube, I hope you don’t mind that I formatted your post with code blocks for readability (triple backtick, ```).

There’s two pieces of information here you should be aware of. Firstly, the canonical method to request a directory index is to request the directory itself (i.e. the index of the web root is /, not /index.txt). Secondly, Caddy’s static file server will enforce this by issuing 301 redirects to requests for index files.

You should instead configure your rewrite as follows:

:2015 {
  tls off
  rewrite {
    to /
  }
}

(I have also restructured it to avoid the regex comparison, but rewrite .* / would also work if you prefer a one-liner in your Caddyfile at the cost of some speed.)

The reason why text.txt works is specifically because it’s not an index file.


(Sugarcube) #3

hi @Whitestrake thanks for getting back so fast

Thanks for the help, it seems to work as you say. Just did some more testing, it appears to be not possible at all to just request an index page explicitly, only via “/”. I didn’t know.

I’m still a little puzzled, on the actual site (linux /docker) i have also a “templates” directive, but templates appear to be not applied on the index.txt (but are applied on index.html). with or without rewrite no difference. When i try to reproduce same locally here on a windows PC, everything works fine. it currently doesn’t make any sense to me, but if i can reproduce it i’ll come back to it.


(Matthew Fay) #4

templates appears to have an ext subdirective that lets you set which file extensions are considered to be templated; have you tried setting that to .txt? (I don’t think the default excludes text files, but maybe it’s worth a shot.) I don’t know why that’d be different on Linux vs. Windows, though. What versions of Caddy are they running, respectively?


(Magikstm) #5

Default does include .txt.

Ref: https://caddyserver.com/docs/templates

Defaults:

Enable templates for all .html, .htm, .tpl, .tmpl, and .txt files

Ref: https://github.com/mholt/caddy/blob/master/caddyhttp/templates/setup.go


(Sugarcube) #6

yes, default should includes. but i tried anyway - no difference.

In the mean time i tried also under WSL (linux on windows) - also can’t reproduce it.
Where it occures is a alpine linux / 32bit / container - so it doesn’t make any sense to me.

I’ll leave it like this as i can’t figure it out why this would be special or what would caust this.


(Sugarcube) #7

Both systems run the current version of caddy on github. (go get + compile yesterday)

yes it is puzzling. my first thought would be difference in config but i checked like a dozen times even with diff /hexdump. I’m probably overlooking something.

At first I didn’t understand you here as I did not see 301’s on the original system. I now realise reproducing it created a different problem because i left the templates out.

So after looking further, i again think there is something not right.

Suppose you launch caddy with a folder with only /index.txt (or /index.html), and enable “templates”

Caddyfile:

127.0.0.1:2015 {
        tls off
        templates
}

if you do HTTP GET of / -> result is 200 ok with content of index.txt
this is ok

if you do HTTP GET of /index.txt -> result is 200 ok with no content.

I think this is not right, or at least unexpected.
If you disable templates:
Caddyfile:

127.0.0.1:2015 {
        tls off
        #templates
}

HTTP GET of /index.txt will return a 301 moved to /

Anyway, i’m not gona spoil your (and my) time any further, and thank you for your feedback and suggestions !!