Rewritte rule in Caddy

(Sredny M Casanova) #1

Hello I have currently a site that needs some rewrite rules, I am migrating that from a Htaccess file to a Caddy instance. By now the file have something like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|map|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.html?path=$1 [NC,L,QSA]

I did this:

    rewrite {
               r !\.(?:css|js|map|jpe?g|gif|png)$
               to {uri} {uri}/ /index.html?path=$1&{query}

but it’s not working as expected

Then, how can a translate that to Caddy valid rewrite rules?

(Matthew Fay) #2

Hi @Sredny_M_Casanova,

It’s already pretty close to working, but Caddy doesn’t use the bang “!” in front of a regex - it needs to match (rather than not match) - in order to get a capture group. You’ve also used $1, which represents the first capture group in nginx - the equivalent in Caddy is {1}, you’ll want to check the docs.

You can do it simpler without regex, though - probably something like this would work:

rewrite {
  ext !css !js !map !jpg !jpeg !gif !png
  to {path} {path}/ /index.html?path={path_escaped}&{query}

(system) #3

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