skip to Main Content

I have a directory (path/to/directory/) in which there is a .htacces blocking access using Require valid-user to require an authentication.
Now my task is to allow access to a subdirectory(path/to/director/foobar) and anything that might be in that subdirectory.
I do know how to grant access to a specific file by matching the request with the file path.
However, I do not know how the structure will look inside the directory.

I was wondering if it may be possible to check with a regular expression for the name of my directory?
so I considered

<RequireANY>
  #Allow subdirectory
  Require %{REQUEST_URI} ^(.*?(bfoobarb)[^$]*)$

  #fallback
  Require valid-user
</RequireANY>

The regex checks out in https://regex101.com/ to match any URI, with foobar in it. Sadly the condition does not work like that. Did I miss something important as I am fairly unfamiliar with .htacces.? Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    I found this solution to to exactly what I needed. I found it in an Installation Guide for Shopware 6 https://docs.shopware.com/de/shopware-6-de/erste-schritte/shopware-6-installieren

    AuthType Basic
    AuthName "Please login."
    AuthUserFile /path/to/.htpasswd
    
    <RequireAny>
        Require expr %{THE_REQUEST} =~ m#.*?s+/foobar.*?#
        Require valid-user
    </RequireAny>
    

  2. You may use this block with <if> expression:

    <If "%{REQUEST_URI} =~ m#^/path/to/directory/(?!foobar/)#">
      <RequireANY>
        AuthType Basic
        AuthName "Secret Area"
        AuthUserFile /full/path/of/passwords/file
        Require valid-user
      </RequireANY>
    </If>
    

    Negative lookahead regex pattern ^/path/to/directory/(?!foobar/) will match /path/to/directory/* except when foobar/ comes directly after it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search