How to Rewrite cachebusting in Caddy v2

In caddy v1 we had this to rewrite cachebusting:

rewrite {
 r ~*(.+)\.(?:\d+)\.(js|css|jpg|svg|png)  
 to {uri} {1}.{2}
}

e.g
The link in html is styles.1234567.css but the file is actually styles.css so we want to load styles.css.

Any ideas where to start? I had a look at path_regexp in this new context, but didn’t really understand how to use it.

It would look something like this:

@cachedFiles {
    not file
    path_regexp cached ~*(.+)\.(?:\d+)\.(js|css|jpg|svg|png)
}
rewrite @cachedFiles {http.regexp.cached.1}.{http.regexp.cached.2}

Might not be exactly right, but that’s the general idea. Basically this says “if the file doesn’t exist on disk and the regexp matches, then rewrite”.

3 Likes

Many thanks again Francis, seems to work OK!. It is actually quite a nice way of writing this stuff, I was just stuggling for examples to work with.

As I mentioned in another post, I’m upgrading some old Kirby 2 CMS sites for Caddy v2. I’ll document it on this community when I’ve done and people can then check to make sure I’ve done it in the best way.

Your time in helping is really appeciated!
Only the ‘Problem with old Kirby 2 Panel rewirstes in Caddy v2’ to fix…

A summary is now in the Wiki - See Caddy v2 file for Kirby 2 & Kirby 3 on local sites for more information.

1 Like

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