Apologies if this question is too simple or in the wrong place but I want to noindex everything but image files on the server. I'm using WordPress as a headless CMS, yet some images are being sourced directly from my server and I still want them indexed, while everything else, I don't need indexed.

I've experimented with the following but my syntax is wrong or semantically I'm breaking some rules:

#<Files ~ "\.(?!png|jpe?g|gif)$">
#    Header set X-Robots-Tag "noindex"

#<FilesMatch ".+\.(gif|jpe?g|png)$">
    Header set X-Robots-Tag "noindex"

As it's currently set up, everything is set to noindex. How do I exclude image formats?


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 a u. 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 regex u.

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"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.