LocationMatch to allow a main url excluding specific sub folder

6.8k Views Asked by At

In the httpd.conf I would like to allow a main path but disallow a particular subfolder.

Any url with this pattern must be accepted:

/app-1.0/public

/app-1.0/images

/app-1.0/

but this one must be ignored / not allowed.

/app-1.0/private

I want to use this configuration on an apache version 2.2.3. I have read that that version does not support negatives regex, but I am not sure.

Many thanks in advance.

1

There are 1 best solutions below

3
Wiktor Stribiżew On

If you want to disallow any paths that start with /app-1.0/ and then have private, you can use

^/app-1\.0/(?!private).*

See the regex demo

Pattern explanation:

  • ^ -start of string (remove if the text is not at the start)
  • /app-1\.0/ - a literal /app-1.0/ text (the dot must be escaped to be treated as a literal dot)
  • (?!private) - a negative lookahead that fails the match (=disallows) if there is a private text right after the current position
  • .* - any 0+ characters other than a newline

NOTE: In my original comment, I suggested /app-1\.0/(?!private$).* pattern with a $ after private. That pattern disallows (=does not match) a string that contains /app-1.0/private at the end ($ is the end-of-string anchor).