0

Yes, I'm stuck still using apache2.2 on a system and I'm trying to use a PCRE with the apache Location directive looking for any URL not containing any images or similar. Here's some things I've tried and it just doesn't seem to do a negative conditional match.

<Location "^/(?!(?:jpe?g|png|bmp|gif|css|js|svg))(.*)">

or

<Location ~ "^.*\.(?!jpg$|png$|bmp$|gif$|css$|js$|svg$)[^.]+$">

Is this sort of thing even possible or is apache limited with this sort of thing?

If my description isn't too clear here's some examples... ;)

I want to match anything like these:

http://some.site.org/something/
https://some.site.org/else/
http://some.site.org
https://some.site.org/else/and/so/on
http://some.site.org/even/something/even/crazy_url/../../././?/

But not match anything like:

http://some.site.org/content/monkey.jpg
http://some.site.org/other/chimp.gif
https://some.site.org/phobia/snake.png
http://some.site.org/junk/style.min.css?v=6.1
https://some.site.org/stuff/juju.js?ver=0.4.1

1 Answer 1

0

Apache docs say 2.2 uses PCRE.

From this question on stackoverflow a lookahead regex should be something like (note the extra padding in .\.js to make it 4 characters)

<Location ~ "^.*(?!\.jpg|\.png|\.bmp|\.gif|\.css|.\.js|\.svg)....$">

This will cause .* to soak up all but the last 4 characters, then check to see if the next four characters does not match any of the extensions, then finish the string.

The problem with this is if the file or path is shorter than 4 characters (/me) it would not match that either. You can try a negative lookbehind match instead:

<Location ~ "^.*$(?<!\.jpg|\.png|\.bmp|\.gif|\.css|\.js|\.svg)">

This should soak up the whole string in .* (There was an argument whether $ should go before or after the lookbehind), then back up and see if it ended in one of the extensions and fail to match if so.

2

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.