Failed to rewrite in beta 13

1. My Caddy version (caddy -version):

v2.0.0-beta.13 h1:QL0JAepFvLVtOatABqniuDRQ4HmtvWuuSWZW24qVVtk=

2. How I run Caddy:

caddy start --config /etc/caddy/caddyfile --adapter caddyfile

a. System environment:

NAME=“openSUSE Tumbleweed”

VERSION=“20200128”

b. Command:

no

c. Service/unit/compose file:

no

d. My complete Caddyfile:

{
experimental_http3
}



starforum.us, www.starforum.us {

templates
encode gzip zstd
file_server
root * /home/starforum/html
php_fastcgi unix//home/php.sock

@a {
        path_regexp a ^/(layouts|m.layouts)/(.+)/(.+).html$
}
rewrite @a /index.php
@b {
        path_regexp b ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$
}
rewrite @b /index.php
@c {
        path_regexp c ^/(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*)
}
rewrite @c /files/{http.matchers.path_regexp.c.2}/{http.matchers.path_regexp.c.3}
@d {
        path_regexp d ^/(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*)
}
rewrite @d /{http.matchers.path_regexp.d.2}/{http.matchers.path_regexp.d.3}
@e {
        path_regexp e ^/(rss|atom)$
}
rewrite @e /index.php?module=rss&act={http.matchers.path_regexp.e.1}
@f {
        path_regexp f ^/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite @f /index.php?mid={http.matchers.path_regexp.f.1}&act={http.matchers.path_regexp.f.2}
@g {
        path_regexp g ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite @g /index.php?vid={http.matchers.path_regexp.g.1}&mid={http.matchers.path_regexp.g.2}&act={http.matchers.path_regexp.g.3}
@h {
        path_regexp h ^/([0-9]+)/(.+)/trackback$
}
rewrite @h /index.php?document_srl={1}&key={2}&act=trackback
@i {
        path_regexp i ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$
}
rewrite @i /index.php?vid={http.matchers.path_regexp.i.1}&document_srl={http.matchers.path_regexp.i.2}&key={http.matchers.path_regexp.i.3}&act=trackback
@j {
        path_regexp j ^/admin/?$
}
rewrite @j /index.php?module=admin
@k {
        path_regexp k ^/([0-9]+)$
}
rewrite @k /index.php?document_srl={http.matchers.path_regexp.k.1}
@l {
        path_regexp l ^/([a-zA-Z0-9_]+)/?$
}
rewrite @l /index.php?mid={http.matchers.path_regexp.l.1}
@m {
        path_regexp m ^/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite @m /index.php?mid={http.matchers.path_regexp.m.1}&document_srl={http.matchers.path_regexp.m.2}
@n {
        path_regexp n ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$
}
rewrite @n /index.php?vid={http.matchers.path_regexp.n.1}&mid={http.matchers.path_regexp.n.2}
@o {
        path_regexp o ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite @o /index.php?vid={http.matchers.path_regexp.o.1}&mid={http.matchers.path_regexp.o.2}&document_srl={http.matchers.path_regexp.o.3}
@p {
        path_regexp p ^/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite @p /index.php?mid={http.matchers.path_regexp.p.1}&entry={http.matchers.path_regexp.p.2}
@q {
        path_regexp q ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite @q /index.php?vid={http.matchers.path_regexp.q.1}&mid={http.matchers.path_regexp.q.2}&entry={http.matchers.path_regexp.q.3}


}

3. The problem I’m having:

  1. Check this working page : https://starforum.us/index.php?mid=board&document_srl=11831
  2. As I’m using rewrite, that page should work in this url : https://starforum.us/board/11831
  3. But, rewrite matcher dosen’t work. in caddy2 beta 9, I used rewrite matcher like this:
matcher q {
        path_regexp q ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite match:q /index.php?vid={http.matchers.path_regexp.q.1}&mid={http.matchers.path_regexp.q.2}&entry={http.matchers.path_regexp.q.3}

so I just fixed to follow changes in beta 13, and now it’s not working.

4. Error messages and/or full log output:

no

5. What I already tried:

read https://caddyserver.com/docs/json/apps/http/servers/errors/routes/match/ this page and https://github.com/caddyserver/caddy/releases/tag/v2.0.0-beta.13 but no luck

6. Links to relevant resources:

V2: Having trouble with rewrite my previous article when beta 9

Hmm. That Caddyfile is quite complex, so I’m not exactly sure what to look for.

Have you read through https://github.com/caddyserver/caddy/issues/2981 ? It’s a more detailed explanation of all the changes in beta 13.

Have a look at the new rewrite docs: https://caddyserver.com/docs/caddyfile/directives/rewrite

And the new matcher docs:https://caddyserver.com/docs/caddyfile/concepts#matchers

You said “it’s not working”. That’s not very clear, so it’s hard to help debug without fully understanding what’s going wrong.

Could you add some code in your index.php to log the request URI as PHP sees it? That’ll help us know if any rewrite gets hit or if none gets hit at all.

Hi,

Thanks for your link. And I read the articles you linked but there’s no luck, too.

Anyway, I’ve just fixed my server failed to serve my site. So I think you may find the difference with https://starforum.us/index.php?mid=board&document_srl=11831 (original url) and https://starforum.us/board/11831 (shortened by rewrite, currently not working).

And this is working nginx rewrite config, you can find in 444 port (https://starforum.us:444/board/11831)

# reserve XE Layout Template Source File (*.html)
rewrite ^/(layouts|m.layouts)/(.+)/(.+).html$ /index.php last;

# conf, query, schema
rewrite ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$ /index.php last;

# static files
rewrite ^/(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*) /files/$2/$3 last;
rewrite ^/(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*) /$2/$3 last;

# rss, blogAPI
rewrite ^/(rss|atom)$ /index.php?module=rss&act=$1 last;
rewrite ^/([a-zA-Z0-9_]+)/(rss|atom|api)$ /index.php?mid=$1&act=$2 last;
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$ /index.php?vid=$1&mid=$2&act=$3 last;

# trackback
rewrite ^/([0-9]+)/(.+)/trackback$ /index.php?document_srl=$1&key=$2&act=trackback last;
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ /index.php?vid=$1&document_srl=$2&key=$3&act=trackback last;

# administrator page
rewrite ^/admin/?$ /index.php?module=admin last;

# document permanent link
rewrite ^/([0-9]+)$ /index.php?document_srl=$1 last;

# mid link
rewrite ^/([a-zA-Z0-9_]+)/?$ /index.php?mid=$1 last;

# mid + document link
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)$ /index.php?mid=$1&document_srl=$2 last;

# vid + mid link
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$ /index.php?vid=$1&mid=$2 last;

# vid + mid + document link
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ /index.php?vid=$1&mid=$2&document_srl=$3 last;

# mid + entry title
rewrite ^/([a-zA-Z0-9_]+)/entry/(.+)$ /index.php?mid=$1&entry=$2 last;

# vid + mid + entry title
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$ /index.php?vid=$1&mid=$2&entry=$3 last;

And this is previously working caddy beta 9 caddyfile.

{
experimental_http3
}

starforum.us, www.starforum.us {

templates
encode gzip zstd
file_server
root * /home/starforum/html
php_fastcgi unix//home/php.sock

matcher a {
        path_regexp a ^/(layouts|m.layouts)/(.+)/(.+).html$
}
rewrite match:a /index.php
matcher b {
        path_regexp b ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$
}
rewrite match:b /index.php
matcher c {
        path_regexp c ^/(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*)
}
rewrite match:c /files/{http.matchers.path_regexp.c.2}/{http.matchers.path_regexp.c.3}
matcher d {
        path_regexp d ^/(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*)
}
rewrite match:d /{http.matchers.path_regexp.d.2}/{http.matchers.path_regexp.d.3}
matcher e {
        path_regexp e ^/(rss|atom)$
}
rewrite match:e /index.php?module=rss&act={http.matchers.path_regexp.e.1}
matcher f {
        path_regexp f ^/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite match:f /index.php?mid={http.matchers.path_regexp.f.1}&act={http.matchers.path_regexp.f.2}
matcher g {
        path_regexp g ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite match:g /index.php?vid={http.matchers.path_regexp.g.1}&mid={http.matchers.path_regexp.g.2}&act={http.matchers.path_regexp.g.3}
matcher h {
        path_regexp h ^/([0-9]+)/(.+)/trackback$
}
rewrite match:h /index.php?document_srl={1}&key={2}&act=trackback
matcher i {
        path_regexp i ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$
}
rewrite match:i /index.php?vid={http.matchers.path_regexp.i.1}&document_srl={http.matchers.path_regexp.i.2}&key={http.matchers.path_regexp.i.3}&act=trackback
matcher j {
        path_regexp j ^/admin/?$
}
rewrite match:j /index.php?module=admin
matcher k {
        path_regexp k ^/([0-9]+)$
}
rewrite match:k /index.php?document_srl={http.matchers.path_regexp.k.1}
matcher l {
        path_regexp l ^/([a-zA-Z0-9_]+)/?$
}
rewrite match:l /index.php?mid={http.matchers.path_regexp.l.1}
matcher m {
        path_regexp m ^/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite match:m /index.php?mid={http.matchers.path_regexp.m.1}&document_srl={http.matchers.path_regexp.m.2}
matcher n {
        path_regexp n ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$
}
rewrite match:n /index.php?vid={http.matchers.path_regexp.n.1}&mid={http.matchers.path_regexp.n.2}
matcher o {
        path_regexp o ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite match:o /index.php?vid={http.matchers.path_regexp.o.1}&mid={http.matchers.path_regexp.o.2}&document_srl={http.matchers.path_regexp.o.3}
matcher p {
        path_regexp p ^/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite match:p /index.php?mid={http.matchers.path_regexp.p.1}&entry={http.matchers.path_regexp.p.2}
matcher q {
        path_regexp q ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite match:q /index.php?vid={http.matchers.path_regexp.q.1}&mid={http.matchers.path_regexp.q.2}&entry={http.matchers.path_regexp.q.3}

}

And I don’t know how to log request URI by adding code in index.php.

Put something like this near the top of your PHP file:

$file = '/tmp/php-uri.log';
$uri = $_SERVER['REQUEST_URI'];
file_put_contents($file, $uri, FILE_APPEND | LOCK_EX);

This should append to a file in /tmp the current request URI on every request.

Hi, this is the php-uri.log results.

original url (https://starforum.us/index.php?mid=board&document_srl=12309) : /index.php?mid=board&document_srl=12309
shortened (style 1, https://starforum.us/12309) : /12309
shortened (style 2, https://starforum.us/board/12309) : /board/12309

Can anyone help me to get through this?

As for now, I think I did a good job at caddyfile but newest caddy returns erroneous results. So should I report this to issue tracker?

What is your current Caddyfile (in its entirety)?

Hi, this is my current info (almost same as I posted before, except the fact I updated caddy beta 13 to beta 14)

caddy version : v2.0.0-beta.14 h1:QX1hRMfTA5sel53o5SuON1ys50at6yuSAnPr56sLeK8=
caddy starting command : caddy start -config /etc/caddy/caddyfile -adapter caddyfile
current caddyfile without any redaction :

{
experimental_http3
}

starforum.us, www.starforum.us {

templates
encode gzip zstd
file_server
root * /home/starforum/html
php_fastcgi unix//home/php.sock

@a {
        path_regexp a ^/(layouts|m.layouts)/(.+)/(.+).html$
}
rewrite @a /index.php
@b {
        path_regexp b ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$
}
rewrite @b /index.php
@c {
        path_regexp c ^/(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*)
}
rewrite @c /files/{http.matchers.path_regexp.c.2}/{http.matchers.path_regexp.c.3}
@d {
        path_regexp d ^/(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*)
}
rewrite @d /{http.matchers.path_regexp.d.2}/{http.matchers.path_regexp.d.3}
@e {
        path_regexp e ^/(rss|atom)$
}
rewrite @e /index.php?module=rss&act={http.matchers.path_regexp.e.1}
@f {
        path_regexp f ^/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite @f /index.php?mid={http.matchers.path_regexp.f.1}&act={http.matchers.path_regexp.f.2}
@g {
        path_regexp g ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$
}
rewrite @g /index.php?vid={http.matchers.path_regexp.g.1}&mid={http.matchers.path_regexp.g.2}&act={http.matchers.path_regexp.g.3}
@h {
        path_regexp h ^/([0-9]+)/(.+)/trackback$
}
rewrite @h /index.php?document_srl={1}&key={2}&act=trackback
@i {
        path_regexp i ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$
}
rewrite @i /index.php?vid={http.matchers.path_regexp.i.1}&document_srl={http.matchers.path_regexp.i.2}&key={http.matchers.path_regexp.i.3}&act=trackback
@j {
        path_regexp j ^/admin/?$
}
rewrite @j /index.php?module=admin
@k {
        path_regexp k ^/([0-9]+)$
}
rewrite @k /index.php?document_srl={http.matchers.path_regexp.k.1}
@l {
        path_regexp l ^/([a-zA-Z0-9_]+)/?$
}
rewrite @l /index.php?mid={http.matchers.path_regexp.l.1}
@m {
        path_regexp m ^/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite @m /index.php?mid={http.matchers.path_regexp.m.1}&document_srl={http.matchers.path_regexp.m.2}
@n {
        path_regexp n ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$
}
rewrite @n /index.php?vid={http.matchers.path_regexp.n.1}&mid={http.matchers.path_regexp.n.2}
@o {
        path_regexp o ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$
}
rewrite @o /index.php?vid={http.matchers.path_regexp.o.1}&mid={http.matchers.path_regexp.o.2}&document_srl={http.matchers.path_regexp.o.3}
@p {
        path_regexp p ^/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite @p /index.php?mid={http.matchers.path_regexp.p.1}&entry={http.matchers.path_regexp.p.2}
@q {
        path_regexp q ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$
}
rewrite @q /index.php?vid={http.matchers.path_regexp.q.1}&mid={http.matchers.path_regexp.q.2}&entry={http.matchers.path_regexp.q.3}


}

thanks for reply.

In beta 13, rewrite became a mutually exclusive directive (by default), meaning that only the first matching rewrite will be evaluated. Some of your rewrites’ matchers overlap, such as @c and @d, I think @f and @g do too. Usually, choosing just one is the desired behavior, since there’s never a need to perform more than one rewrite. The only exception would be in some (I think rare, convoluted) cases to reduce duplication in the config, in which case you might want rewrites to cascade.

Do your rewrites need to cascade (i.e. rewrite one into another and perform multiple rewrites)?

Keep in mind that the php_fastcgi directive also adds a rewrite with try_files: https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#expanded-form

Can you give some examples of actual requests, what they are rewritten to (i.e. what PHP sees), and what you expect the result to be? (Maybe a table would help organize the info.)

@nginx_x Oh, I think I see a problem. Your placeholders look like this:

{http.matchers.path_regexp.n.1}

But they should look like this:

{http.regexp.n.1}

I don’t remember when that changed (sorry).

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