1. Caddy version (caddy version
):
/srv # caddy version
v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=
2. How I run Caddy:
caddy run -adapter caddyfile -config caddyfile
a. System environment:
MasOS 10.15.6
b. Command:
caddy run -adapter caddyfile -config caddyfile
c. Service/unit/compose file:
n/a
d. My complete Caddyfile or JSON config:
{
admin off
auto_https disable_redirects
http_port 1314
}
http://localhost:1314 {
file_server
root * {env.PWD}/www
# --- Method 1
@post {
file /blog/{path}index.html /blog/{path}/index.html
not file {path} {path}/
path_regexp post ^/([^/]+)/?$
}
# --- Method 2
#@post {
# file /blog/{path} /blog/{path}/
# not file {path} {path}/
# path_regexp post ^/([^/]+)/?$
#}
redir @post /blog/{re.post.1}/
templates
}
3. The problem Iβm having:
Please refer to end of post.
4. Error messages and/or full log output:
N/A
5. What I already tried:
2 different methods give unexpected results for different test cases
6. Links to relevant resources:
https://github.com/J-Siu/caddy-test.git
Test
The whole dir tree with caddyfile is in github:
All text/html files are single line text stating file path with no html tags. You can check on github before cloning.
git clone https://github.com/J-Siu/caddy-test.git
cd caddy-test
caddy run -adapter caddyfile -config caddyfile
Want to do
www/
βββ blog/
β βββ blogdir/
β β βββ index.html
β βββ blogfile
β βββ dir2dir/
β β βββ index.html
β βββ dir2file
β βββ file2dir/
β β βββ index.html
β βββ file2file
βββ dir/
β βββ index.html
βββ dir2dir/
β βββ index.html
βββ dir2file/
β βββ index.html
βββ file
βββ file2dir
βββ file2file
I want:
Request | Redirect |
---|---|
if /X(file/dir) does not exist | if /blog/X(file/dir) exist |
Base on above dir tree, following should happen
Request | Redirect |
---|---|
http://localhost:1314/dir | no redirect (does not exit in /blog) |
http://localhost:1314/dir2dir | no redirect (exist at /) |
http://localhost:1314/dir2file | no redirect (exist at /) |
http://localhost:1314/file | no redirect (does not exit in /blog) |
http://localhost:1314/file2dir | no redirect (exist at /) |
http://localhost:1314/file2file | no redirect (exist at /) |
http://localhost:1314/blogdir | http://localhost:1314/blog/blogdir |
http://localhost:1314/blogfile | http://localhost:1314/blog/blogfile |
Method 1
caddyfile
{
admin off
auto_https disable_redirects
http_port 1314
}
http://http://localhost:1314 {
file_server
root * {env.PWD}/www
# --- Method 1
@post {
file /blog/{path}index.html /blog/{path}/index.html
not file {path} {path}/
path_regexp post ^/([^/]+)/?$
}
redir @post /blog/{re.post.1}/
}
Result:
Test# | Request | Expected | Actual |
---|---|---|---|
M1T1 | http://localhost:1314/dir | no redirect (does not exit in /blog) | no redirect |
M1T2 | http://localhost:1314/dir2dir | no redirect (exist at /) | http://localhost:1314/blog/dir2dir/ |
M1T3 | http://localhost:1314/dir2file | no redirect (exist at /) | no redirect |
M1T4 | http://localhost:1314/file | no redirect (does not exit in /blog) | no redirect |
M1T5 | http://localhost:1314/file2dir | no redirect (exist at /) | no redirect |
M1T6 | http://localhost:1314/file2file | no redirect (exist at /) | no redirect |
M1T7 | http://localhost:1314/blogdir | http://localhost:1314/blog/blogdir | http://localhost:1314/blog/blogdir |
M1T8 | http://localhost:1314/blogfile | error 404(no /blog/blogfile/index.html) | error 404 |
Method 2
caddyfile (removed index.html
in file matching)
{
admin off
auto_https disable_redirects
http_port 1314
}
http://http://localhost:1314 {
file_server
root * {env.PWD}/www
# --- Method 2
@post {
file /blog/{path} /blog/{path}/
not file {path} {path}/
path_regexp post ^/([^/]+)/?$
}
redir @post /blog/{re.post.1}/
}
Result:
Test# | Request | Expected | Actual |
---|---|---|---|
M2T1 | http://localhost:1314/dir | no redirect (does not exist in /blog) | no redirect |
M2T2 | http://localhost:1314/dir2dir | no redirect (exist at /) | no redirect |
M2T3 | http://localhost:1314/dir2file | no redirect (exist at /) | http://localhost:1314/blog/dir2file |
M2T4 | http://localhost:1314/file | no redirect (does not exist in /blog) | no redirect |
M2T5 | http://localhost:1314/file2dir | no redirect (exist at /) | no redirect |
M2T6 | http://localhost:1314/file2file | no redirect (exist at /) | no redirect |
M2T7 | http://localhost:1314/blogdir | http://localhost:1314/blog/blogdir | no redirect |
M2T8 | http://localhost:1314/blogfile | http://localhost:1314/blog/blogfile | http://localhost:1314/blog/blogfile |
Issue
Why:
Method 1 test M1T2 failed?
Method 2 test M2T3, M2T7 failed?