4

I have tried writing this into my config file, to match folders only in that 'example' directory and no further sub-folders...

<DirectoryMatch "\/var\/www\/html\/example\/[^/]+\/">
    IndexIgnore ..
</DirectoryMatch>

Other regex I have tried...

/var/www/html/example/[^/]+/
/var/www/html/example/(.+/){1}

If I explicitly write the path to the folder, it works. Where am I going wrong?

I see Apache uses Perl Compatible Regular Expressions provided by the PCRE library.


Server version: Apache/2.4.18 (Ubuntu)

Server built: 2016-07-14T12:32:26

ckhatton
  • 1,359
  • 1
  • 14
  • 43

1 Answers1

3

/ does not need to be escaped. Use ^ and $ to mark the start and end of the matched path. The trailing slash must be optional (/?).

<DirectoryMatch "^/var/www/html/example/?$">
  ...
</DirectoryMatch>

or

<DirectoryMatch "^/var/www/html/example/[^/]+/?$">
  ...
</DirectoryMatch>
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • You're a star! Picking it apart, it seems I just needed the `$` symbol at the end (I am surprised that it is not documented [here](https://httpd.apache.org/docs/current/mod/core.html#directorymatch)) – ckhatton Jan 18 '17 at 10:17
  • I only noticed it working though when I changed the `IndexIgnore` to something like `Options -Indexes`. Do you know why `IndexIgnore` does not want to work? – ckhatton Jan 18 '17 at 10:17
  • Why must the trailing slash be optional? – ckhatton Jan 18 '17 at 10:34
  • 1
    Says so in [the documentation](https://httpd.apache.org/docs/2.4/mod/core.html#directorymatch): *This directive applies to requests for directories that may or may not end in a trailing slash, so expressions that are anchored to the end of line ($) must be written with care.* – Markus Jarderot Jan 18 '17 at 11:59
  • Ar okay. Thank you – ckhatton Jan 18 '17 at 17:01