WonderCMS Rewrite

I’m trying to get WonderCMS setup on Caddy 10.11, but I’m struggling with the Apache htaccess or nginx config to Caddy rewrites. WonderCMS comes with the .htaccess file, but they posted the nginx config in their wiki:

location ~ database.js {
	return 403;
}

autoindex off;

location / {
	if (!-e $request_filename) {
		rewrite ^/(.+)$ /index.php?page=$1 last;
	}
}

I’ve gotten the database.js section rewritten as:

internal /forbidden
rewrite {
    r /new/(database.js)
    to /forbidden
}

But I’m struggling with the bottom section. Should it be:

rewrite {
    r ^/(.+)$
    to /index.php?page={1}
}

What about the !-e $request_filename or last?

EDIT
I should mention this test site is in a /new/ subdirectory, that’s why the database.js has /new/ in the path.

According to the nginx documentation, -e checks for the existence of a file, directory, or symbolic link. !-e then implies that we proceed with the rewrite only if it doesn’t exist.

Caddy doesn’t at present have these kinds of explicitly written conditionals, but looking into the way rewrite works, we can achieve the same result:

destinations… is one or more space-separated paths to rewrite to, with support for request placeholders as well as numbered regular expression captures such as {1}, {2}, etc. Rewrite will check each destination in order and rewrite to the first destination that exists. Each one is checked as a file or, if ends with /, as a directory. The last destination will act as default if no other destination exists.

https://caddyserver.com/docs/rewrite

This behaviour results in the common usage of this form of rewrite:

rewrite {
  to {path} {path}/ /index.php
}

With some slight adaptation to suit the nginx equivalent you gave:

rewrite {
  r ^/(.+)$
  to {path} {path}/ /index.php?page={1}
}

As for last, well, that just tells nginx that this is the final rewrite we want it to carry out at this location, and to carry on actually serving the request. You don’t need to tell Caddy this - the format of the rewrite directive in your Caddyfile tells us all we need to know.

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