I’m using Caddy for a WordPress site without problem for several months (so first, thanks for this !).
Recently however, I noticed a problem with the Rest API which is more and more used in WordPress. I noticed the search through the Rest API is not correct. I always have the latest posts instead of search results, which is the default if you provide no argument for search.
Furthermore, If I add parameter to the search beyond just a keyword, the operation fails and I have a 400 error. I think, without certainty, it is linked to Caddy and the necessary rewrite for WordPress. Here’s the relevant part of my caddyfile :
rewrite / {
if {path} not_match ^\/wp-admin
to {path} {path}/ /index.php?_url={uri}
}
I tried to exclude URL with wp-json as well, but then I have a 404 sent by Caddy.
That combined with your comment made me wonder whether the rewrite is working correctly at all …
The {uri} placeholder as per the Caddy docs https://caddyserver.com/docs/placeholders is “The request URI (includes path, query string, and fragment)” which suggests that maybe your rewrite is rewriting it to /index.php?_url=/wp-json/wp/v2/posts?search=nolan maybe with the two ? separators. The Caddy docs say there is a {uri_escaped} placeholder that you can use - give that a go maybe?
It’s hard to fully debug when I don’t have a Wordpress site to play with, but these suggestions may point you in the right direction. Let me know how you get on.
Unfortunately, I have no easy way to try without Caddy, I have a server without ngninx or Apache…
But you’re right that the rewrite is the problem. I tried the {uri_escaped}which I didn’t know, but it prevents search to work at all, all REST API requests display the last posts, as if I had not made any search.
I think though the answer is here somewhere.
EDIT : I don’t think it could help, but here’s what I have on the log side :
Hrm, well. That is a perfectly legal query string, I suppose.
Out of curiosity, why are we rewriting to include ?_url={uri} anyway? I can’t for the life of me find out where it’s documented to use this format, even though I’ve seen a number of people use it.
I mean, the Apache htaccess rewrites just to index.php, and the NGINX-specific documentation rewrites to /index.php?$args, the Caddy equivalent of which is /index.php?${query}, which matches the example from Caddy’s WordPress example on GitHub: examples/Caddyfile at master · caddyserver/examples · GitHub - so where does the _url paramenter come from?
So far, I have noticed one other bugfix using WordPress : now I can edit a published post and preview my edits in a different window. Until now, I had to publish changes to see them online.
As far as I’m concerned, it’s a great change. I’m glad the example on GitHub was changed, WordPress support is really better that way.