Same rules for multiple directories in Apache?

I have 2 subdomains that use the same rules as below:

<Directory /srv/project/sites/project.hu/htdocs/>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\?*$ index.php?route=$1 [L,QSA]
    SetEnv config default,local
    Order allow,deny
    allow from 192.168.0.0/16
</Directory>

<Directory /srv/project/sites/admin.project.hu/htdocs/>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\?*$ index.php?route=$1 [L,QSA]
    SetEnv config default,local
    Order allow,deny
    allow from 192.168.0.0/16
</Directory>

As you can see, the rules are the same in both containers. How can I specify these rules in one container? At first I thought about this:

<DirectoryMatch ^/srv/project/sites/(?:(?:admin\.project\.hu)|project\.hu)/htdocs/$>
 ...
</DirectoryMatch>

But is there no way to do this in a cleaner way that I am missing?

Edit: I don't like the DirectoryMatch path, because when I have more directories, the regex will grow unattainable.

+3
source share
2 answers

translate the rewrite rules into a separate file, for example. rewrite.confand include like this:

<Directory /srv/project/sites/project.hu/htdocs/>
  Include rewrite.conf
</Directory>

<Directory /srv/project/sites/admin.project.hu/htdocs/>
  Include rewrite.conf
</Directory>
+7
source
<DirectoryMatch ^/srv/project/sites/(?:admin\.)?project\.hu/htdocs/$>

It seems pretty clean to me ...

+1
source

Source: https://habr.com/ru/post/1705442/


All Articles