# Regex for Apache Directory directive

**URL:** https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089
**Category:** Offtopic
**Created:** [March 18, 2025, 9:55am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089 "2025-03-18T09:55:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 18, 2025, 9:55am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/1 "2025-03-18T09:55:55Z")

</div>

I am trying to set up a DAV access to our computational server…

If I name individual `Directory` directives it works:

```julia
    <Directory /home/userfoo>
      DAV On
      AuthName "webdav"
      AuthType Basic
      AuthBasicProvider external
      AuthExternal pwauth
      Require user userfoo
      AssignUserID userfoo userfoo
      Options +Indexes
      AllowOverride all
    </Directory>

    <Directory /home/davtest>
      DAV On
      AuthName "webdav"
      AuthType Basic
      AuthBasicProvider external
      AuthExternal pwauth
      Require user davtest
      AssignUserID davtest davtest
      Options +Indexes
      AllowOverride all
    </Directory>

    [...]

```

I believe I can use RegEx in the directive, but how?  
Something like this (that doesn’t work):

```julia
    <Directory ~ "/home/*+">
      DAV On
      AuthName "webdav"
      AuthType Basic
      AuthBasicProvider external
      AuthExternal pwauth
      Require user $1     
      AssignUserID $1 $1     
      Options +Indexes
      AllowOverride all
    </Directory>

```

Any help ?

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [March 18, 2025, 10:17am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/2 "2025-03-18T10:17:13Z")

</div>

I think there’s an error in your regex: `*` is a quantifier, not a “wildcard” symbol. You might want to change the expression to `^/home/.+$` or even `^/home/[a-z]+$`.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 18, 2025, 10:19am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/3 "2025-03-18T10:19:01Z")

</div>

> [@sylvaticus](#):
>
> I believe I can use RegEx in the directive, but how?  
> Something like this (that doesn’t work):

Thank you, you are right. But how can I save the part after “/home/” to use inside the directive (if it is possible) ?

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [March 18, 2025, 10:28am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/4 "2025-03-18T10:28:07Z")

</div>

It looks possible with [`DirectoryMatch`](https://httpd.apache.org/docs/2.4/mod/core.html#directorymatch):

> From 2.4.8 onwards, named groups and backreferences are captured and written to the environment with the corresponding name prefixed with “MATCH\_” and in upper case. This allows elements of paths to be referenced from within expressions and modules like `mod_rewrite`. In order to prevent confusion, numbered (unnamed) backreferences are ignored. Use named groups instead.

So in your case (added optional trailing `/` too):

```julia
<DirectoryMatch "^/home/(?<username>[a-z]+)/?$">
    Require user %{env:MATCH_USERNAME}
</DirectoryMatch>

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 18, 2025, 10:41am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/5 "2025-03-18T10:41:21Z")

</div>

> [@barucden](#):
>
> ```julia
> Require user %{env:MATCH_USERNAME}
> 
> ```

Thank you.. this seems promising, but it also seems that `Require` got somehow interpreted before the placeholder is expanded:

```julia
# apachectl configtest
AH00543: apache2: bad user name %{env:MATCH_USERNAME}

```

(Apache 2.4.52)

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 18, 2025, 11:49am UTC](https://discourse.julialang.org/t/regex-for-apache-directory-directive/127089/6 "2025-03-18T11:49:47Z")

</div>

I solved it with mod\_perl:

```julia
<Perl>
     my $group = "users";
     my $output = `members $group`;
     chomp($output);
     my @members = split(/\s+/, $output);
     foreach ( @members ) {
       push @PerlConfig, qq|
<Directory /home/$_ >
      DAV On
      AuthName "webdav"
      AuthType Basic
      AuthBasicProvider external
      AuthExternal pwauth
      Require user $_
      AssignUserID $_ $_
      Options +Indexes
      AllowOverride all
</Directory> |;
}
</Perl>

```
