Configuring rewrite in Caddy web server does not work

32 Views Asked by At

I am trying to configure a rewrite rule in Caddy web server:

From: /data/id/name.html

To: /data.py?id=id&name=name

I have tried different approaches:

 rewrite /data/([^/]+)/([^/]+)\.html /data.py?id={1}&name={2}

This does not work and returns the homepage

  rewrite /data/(\d+)/([A-Za-z0-9_-]+)\.html /data.py?id={1}&name={2}

This also brings me to the homepage

  rewrite ^/data/(\w+)/(\w+)\.html$ /data.py?id={1}&name={2}

This fails and Caddy won't start due to an error

    rewrite /data {
           r ^/(\w+)/(\w+)\.html$
           to /data.py?id=$1&name=$2
    }

Also fails when Caddy won't start. How do I configure it correctly? And why creating rewrite rules always fails to me; I know I am not smart :-) but I think it shouldn't be that difficult.

1

There are 1 best solutions below

0
aviv On

I found the solution:

    @zxp {
            path_regexp myregex ^/data/([^/]+)/([^/]+)\.html$
    }
    rewrite @zxp /data.py?id={re.myregex.1}&name={re.myregex.2}