Redirect everything to CDN Url exept *.html and .htm files

I would like to serve all .html and .htm files from caddy locally and redirect all other requests to a CDN URL on a different Server.

I want them to be redirected and not proxied.

How can I achieve this? I tried several RegEx Versions with no luck.

Thannk you for any input!

After some more digging in the forum and config documentation i came up with the following:

@cdn {
  not path / */ /*.html /*.htm
}
redir @cdn https://cdn.local/{path} permanent

FYI, using infix wildcards does not cross / boundaries, so this would only work for top-level HTML files and not those inside directories. If you use *.html *.htm instead, it should match any path that ends in that extension.

You should use {uri} instead, because {path} won’t contain the query, if there was one on the request. Also, you should remove the trailing slash in there, because the uri/path will already contain a leading slash.

You can also save a couple lines by using the single-line named matcher syntax.

So altogether, this is what I recommend:

@cdn not path / */ *.html *.htm
redir @cdn https://cdn.local{uri} permanent

Thank you a lot for your input! This helps me a great way.

1 Like

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