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
MikeyinTN
  • 1
  • 2

1 Answers1

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.

DerfK
  • 19,313
  • 2
  • 35
  • 51
  • Thanks, that gets me 95% the way there, but it looks like URLs like this will still be matched then: http://some.site.org/junk/style.min.css?v=6.1 https://some.site.org/stuff/juju.js?ver=0.4.1 e.g. Here's the example on [REGEX101.com] (https://regex101.com/r/wJ6bD5/6) – MikeyinTN Jul 12 '16 at 20:17
  • I don't *think* `` considers the query part of the URL (`?v=blah` is ignored), but you should test in Apache. – DerfK Jul 12 '16 at 20:37