rewrite / {
regexp ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$
to /index.php?vid={1}&mid={2}&document_srl={3}
}
3. The problem Iām having:
I cannot adapt caddyfile.
4. Error messages and/or full log output:
adapt: parsing caddyfile tokens for ārewriteā: Caddyfile:9 - Error during parsing: Wrong argument count or unexpected line ending after ārewriteā
5. What I already tried:
FYI, original apache rewrite was:
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ ./index.php?vid=$1&mid=$2&document_srl=$3 [L,QSA]
converted for nginx from apache rewrite was:
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ /index.php?vid=$1&mid=$2&document_srl=$3 last;
and I read about some forum articles but I cannot understand why Iām failing to adapt my config.
I tried to rewrite it by using matcher, but thereās no luck too.
Using a matcher should be the way to go here. The rewrite youāre using is v1-specific configuration and wonāt work in v2. Can you post the config you tried with the matcher?
Iām actually seeing two problems here. First is the arg count issue, which Iām not sure about. That should be the correct method to use rewrite. It may be a different error provoking this error message.
I hit up @francislavoie for second set of eyes on this and he pointed out a second issue, which is that to address the capture groups, you actually need different placeholders in v2:
If you are using regexp matchers, capture groups (both named and numeric) are available as well:
So {1} in the rewrite target should be {http.matchers.a.???.1}, where ??? should be a name youāve given for the regex - but the documentation doesnāt specify a method to name a regex in a matcher. The regex name is able to be provided in JSON configuration, so you might need to change away from the Caddyfile to JSON to get it working until we sort that question out.
Perhaps @matt can provide some insight into this - both the error and the regex name?
I ninja edited it in after the initial post, but if youād like, you could try learning the JSON configuration and setting it up that way. You can adapt the Caddyfile without the rewrite in it to start with, then add the rewrite configuration in JSON, which should be fully functional. Otherwise, yeah, hang ten, weāll look into it.
Bit of advice - Google one of those online JSON editors, theyāll keep your structure nice and neat and present it to you in a way thatās easy to read.
Yes, itās undocumented that regexes can have names specified before them. My bad. Will fix that now.
Also, yes, I agree these placeholders are WAY too long, I will have to see how we can trim them down. The Caddyfile gives us the option to make āshorthandā placeholders. It already has several: Home Ā· caddyserver/caddy Wiki Ā· GitHub - we might be able to make some that work for these cases (like {myregex.2} and just assume that itās a path regex? I dunno).
Sorry it wasnāt clear. Keep the conversation going so I can work on improving it this week.
(Thank you for using v2 while it is still in beta!!)
So Iāve found my typo, which caused parsing error. I tried to call a matcher by just name, but I need to call it by match:name! That was a simple misunderstanding of documentation.
And your example helped me a lot. Finally, Iāve just got to activate my little website, after rewriting 17 rules. This is one-time work so I donāt have anything to complain.
But, it would be lovely if I call rewrite target as {1} {2} just like old days.
(And I found that I cannot use matcher {} in one line, so I seperated into 3 lines like you did.)
Iām wanting to improve this, both the documentation and the implementation. What would make matchers easier?
I would love this too, but the problem is, how does it know which regex the capture numbers are referring to? Matchers in Caddy 2 can have more than one regexā¦
Ah yes, we like to keep things consistent and clean.
After many thoughts, I think current system is sufficiently effective, at least for me.
I considered some ideas:
call matcher by matcher:name, not match:name, for intuitive use
it is not a crucial factor, and I donāt think it will be effective that much.
maybe caddy v2 could implement two method at the same time, like using matcher(current) and just one regexp-specific rewrite(maybe, rrewrite).
but would it be worth it? Idk
Maybe the confusion is coming from matcherās versatility and flexibility, or lack of basic example conf which Iāve mentioned in Github Issue #2858
Some of real world examples will be a great addition to documentation, sometimes I feel the docs are presenting syntax only.
But I donāt have any more specific thoughts, maybe due to lack of caddy experience(I used caddy just for 2 week!). I need to dig into caddy a lot more if I want to propose an alternative implementation.
Thank you for your thoughtful consideration of this, @nginx_x ā really appreciate it.
I can understand the disparity between the matcher directive and using the matchers with match: instead of matcher:. I will think about it tooā¦ but yeah, not sure if it will be that effective.
We only have an ad-hoc reference for Caddy 2, we havenāt written its documentation yet. Will definitely get to that soon, itās a requirement before we launch it.