Having trouble with regex redirect

1. Caddy version (2.2.1):

2. How I run Caddy:

Debian 10 standard from the default systemd install

My complete Caddyfile or JSON config:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.
:8080

# Set this path to your site's directory.
root * /var/www/red-service/www

# Enable the static file server.
file_server

# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

php_fastcgi unix//run/php/php7.3-fpm.sock

@test{
  path_regexp test ^/test/(.+)/(.+)/([0-9]+)$
}
rewrite @test /test.php?m={http.regexp.test.1}&i={http.regexp.test.2}&mid={http.regexp.test.3}

3. The problem I’m having:

It’s insisting that path regexes are invalid

4. Error messages and/or full log output:

Nov 26 13:57:49 tracker-01 caddy[27665]: run: adapting config using caddyfile: /etc/caddy/Caddyfile:30: unrecognized directive: path_regexp

Initially i tried other directives that didn’t work until i found that the rewrite directives changed a lot between v1 and 2. This is my latest attempt. Unless i’ve misread the docs this should work.

What am i doing wrong? Do i need to install something extra to have regex redirections?

Replying to myself: Adding a space after the matcher name made it work:
@test{
versus
@test {

This is the second time a missing space on a Caddyfile has broken it for me (last time it was when using multiple domains on a file, it’s domain, domain, not domain,domain). I think it would be worth it to be very specific on the documentation where these spaces are needed

2 Likes

It is mentioned in the docs that whitespace is significant for tokenizing:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.