Apache动态目录配置问题:DirectoryMatch指令为何失效?
Let’s cut to the chase: the problem is all about when Apache applies DirectoryMatch rules versus regular Directory rules, paired with how your GitLab-deployed review directories get created after Apache is already running.
Here’s the breakdown:
- DirectoryMatch runs once at Apache startup: When you launch Apache, it scans your entire filesystem to find every directory that matches the regex in your DirectoryMatch rule. If your
review-15-connexi-d7rgbxdirectory is created after Apache starts (which is exactly how dynamic GitLab deployments work), Apache never picks it up during that initial scan. That means none of your DirectoryMatch settings—likeAllowOverride AllorDirectoryIndex app_dev.php—get applied to this new directory. - Regular Directory rules are path-based, not filesystem-scanned: When you use
<Directory "/var/www/REVIEW/review-15-connexi-d7rgbx/web">, Apache doesn’t care if the directory exists when it starts. It just stores that path and applies the configuration whenever a request hits that path—even if the directory was created later. That’s why this works when DirectoryMatch doesn’t.
Fixes for your dynamic environments:
You’ve got two straightforward options here:
Switch to a wildcard Directory directive (recommended)
Apache supports shell-style wildcards in Directory blocks, which are evaluated at request time (not startup). Replace your DirectoryMatch with this:<Directory "/var/www/REVIEW/review-*/web"> DirectoryIndex app_dev.php Options Indexes FollowSymLinks MultiViews AllowOverride All </Directory>This will match any
review-*directory under/var/www/REVIEW/followed by/web—perfect for your dynamic review environments, no matter when they’re created.Reload Apache after deployment (less ideal)
If you really want to stick with DirectoryMatch, add a step in your GitLab CI pipeline to reload Apache right after creating the review directory. This forces Apache to re-scan the filesystem and apply the DirectoryMatch rules to the new directory. Just keep in mind that reloading Apache can disrupt other sites on the server, so this isn’t the best long-term solution.
How to confirm this was the issue:
Check your Apache error logs—you’ll probably see entries like AllowOverride not allowed or File does not exist for the /login path. That’s because without the DirectoryMatch (or wildcard Directory) config applied, Apache uses default settings (like AllowOverride None), which blocks Symfony’s .htaccess from rewriting URLs correctly. Hence the 404 when trying to access /login.
内容的提问来源于stack exchange,提问作者Emmanuel de Folleville

