Search code examples
regexapacheauthentication.htpasswdlocationmatch

apache LocationMatch and Basic Auth


Is there any way to get matched substring from LocationMatch and use it as part of configuration? I have several similar sites that uses apache Basic auth and want to check against "sitename".passwd files..

in code I mean this but working:

<LocationMatch /([^/]+)/login>
  AuthType Basic
  ...
  AuthUserFile /var/sitepwds/$1.passwd
</LocationMatch>

Solution

  • Use mod_macro for that.

    Write:

    <Macro MyMacro $name>
    <LocationMatch /$name/login>
      AuthType Basic
      ...
      AuthUserFile /var/sitepwds/$name.passwd
    </LocationMatch>
    </Macro>
    

    and use it like:

    Use MyMacro site1
    Use MyMacro site2
    Use MyMacro site3
    ...