Exclude image formats from Files directive in .htaccess
Solution 1:
Your regular expression matches files ending with .gif
, .jpg
, .jpeg
& .png
, but you need a regular expression that matches everything but them. This is possible with a negative lookahead regular expression. From Jan Goyvaerts' regular-expressions.info:
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a
q
not followed by au
. Negative lookahead provides the solution:q(?!u)
. The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regexu
.
So here, you'd replace the u
with your .*\.(gif|jpe?g|png)$
, resulting in something like:
<FilesMatch "^(?!.*\.(gif|jpe?g|png)$).*$">
Header set X-Robots-Tag "noindex"
</FilesMatch>