1

I am trying to build a regular expression in apache which match with all files with extensions as .html, .css, .js, .jpg, etc... except it the url has the word "nocache"

I have read other entries in stackoverflow, and I've create the following regular expresion

<FilesMatch "^(.*(?!nocache)\.(png|bmp|jpg|gif|html|htm|css|js|ttf|svg|woff|txt))$">
  ExpiresActive on
  ExpiresDefault "now plus 1 month"
</FilesMatch>

The problem is that this regular expression is not working fine. All files with extensions are being cached, but the file with the word "nocache" is being cached too.

Does someone see what is the problem?

stema
  • 90,351
  • 20
  • 107
  • 135

1 Answers1

1

That is because you put the lookahead assertion in the wrong place

^(?!.*nocache).*\.(png|bmp|jpg|gif|html|htm|css|js|ttf|svg|woff|txt)$

When you place it before the dot, it will look from that position ahead and all it sees is the file extension, that is not "nocache", so it is true.

In my expression it is placed after the anchor and has its own .*, so it will look from the start of the string, if there is "nocache" anywhere in the string.

stema
  • 90,351
  • 20
  • 107
  • 135