Regex in a Pages _headers file?

Question about using a _headers file with a Pages project: does it support regex? For example, would the following work?

/(.*).(ac3|avi|bmp|br|bz2|css|cue|dat|doc|docx|dts|eot|exe|flv|gif|gz|ico|img|iso|jpeg|jpg|js|json|map|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|webp|webm|webmanifest|woff|woff2|xls|xlsx|xml|zip)
    Cache-Control: public, max-age=31536000, immutable
    CDN-Cache-Control: public, max-age=31536000, immutable
    X-Content-Type-Options: nosniff

I currently use a Worker for this, but after one of my posts went semi-viral yesterday and blew through my monthly limit, am wondering whether I’m better off doing it through _headers if that’s possible.

(Also asked on the Discord.)

Unfortunately not, you can’t use Regex in _headers.

The logic for parsing _headers is open source in the Pages asset-server if you wanted to take a look: https://github.com/cloudflare/workers-sdk/blob/5e67ea176ac03718061b49bce6311ce169a355bd/packages/pages-shared/asset-server/rulesEngine.ts#L27

1 Like

Will do. Thanks!

Although not officially supported, it does seem that the regex string is just concatenated, so you might be able to get something working: https://github.com/cloudflare/workers-sdk/blob/5e67ea176ac03718061b49bce6311ce169a355bd/packages/pages-shared/asset-server/rulesEngine.ts#L58

1 Like

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