3

I am trying to set a bunch of url-based rules in Apache with LocationMatch and the regex seems to behave in a way I do not understand.
I tried looking up the documentation but it isnt very clear to me WRT to my issue... nor are the other question/answers around the interwebs.

Here is my Location directive and a generic example of the structure of my LocationMatch directives:

<Location />
    Header set X-Intelligence "CatchAll"
</Location>

<LocationMatch "\.(pdf)$">
    Header set X-Intelligence "pdf1"
</LocationMatch>
  • These directives are all inside a virtual host with ssl.
  • I am not using any other Location or LocationMatch directives anywhere else in the configs.
  • The LocationMatch directives all come after my CatchAll Location directive.
  • Everything works normally when I use just the <Location /> directive alone.

I have noticed some differences between the regex used here and the regex I have experience with..

  • Using start/end slashes doesn't seem to work.
  • Using ending modifier tags like /i /g do not work.
  • Other behavioral differences given below

I have tried many different things but here are some examples of my results:

1.

<LocationMatch "(?i)\.(pdf)$">
    Header set X-Intelligence "pdf1"
</LocationMatch>
  • The above seems to match all .pdf and .PDF extension URLs.

However:

<LocationMatch "\.(pdf)$">
    Header set X-Intelligence "pdf1"
</LocationMatch>
  • Does not match any .pdf extension URLs.
  • .pdf do not get caught by the CatchAll (displays 404 not found).
  • .PDF (capitals) do get caught by the CatchAll.

.
2. Not similarly:

<LocationMatch "\.(docx?)$">
    Header set X-Intelligence "doc2"
</LocationMatch>
  • The above matches my .docx URLs.

However:

<LocationMatch "\.(doc)$">
    Header set X-Intelligence "doc2"
</LocationMatch>

<LocationMatch "\.(docx)$">
    Header set X-Intelligence "doc22"
</LocationMatch>
  • The above does not match .docx URLs.
  • Does not get caught by the CatchAll (404 not found).
  • I have tried rearranging the order and still gives same results.

Not sure what is going on but the behavior is totally not what I expect.
Anyone have any ideas on what I could be doing wrong or what I am not understanding?

Open to any suggestions to make this question better. Thanks.

agent provocateur
  • 824
  • 3
  • 19
  • 39
  • I can't replicate with Apache 2.2, I suspect something else is interfering. I tried `(?i)\.(docx?)$` as well as `(?i)\.docx?$`. Since you aren't capturing you can get rid of `(` and `)`. This is not really a programming question and is probably better suited to [sf] – miken32 Apr 12 '17 at 22:02
  • hmm ok thanks for trying to replicate. – agent provocateur Apr 13 '17 at 18:22

0 Answers0