How to make a rewrite rule of "Redirect Trailing Slashes If Not A Folder."

So, these two, then:

  1. If the client requests an existing file, but with a trailing slash, redirect them to remove the slash.
  2. If they don’t request an existing file, serve them to the index file without redirecting.

Lets refactor a bit, then. Try something like this.

# Check for:
# 1. File without trailing slash (like -f)
# 2. Existing directory (like -d)
# Else, fall back to PHP index
rewrite {
  r ^(.+?)/?$
  to {1} {1}/ /index.php?{1}
}

# If we got a request with an unneeded
# trailing slash, redirect the client
# - don't redirect PHP index, though
redir {
  if {uri} ends_with /
  if {rewrite_uri} not_ends_with /
  if {rewrite_uri} not_starts_with /index.php?
  / {rewrite_uri}
}