Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same rules for multiple directories in Apache?

I have 2 subdomains which use the same rules, as seen 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 a single container? At first I thought about using this:

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

But isn't there a way to do this in a more clean way that I'm missing?

Edit: I dislike the DirectoryMatch way, because when when I'll have more directories, the regex will grow unmaintainable.

like image 577
K. Norbert Avatar asked Jan 27 '26 21:01

K. Norbert


2 Answers

put the rewrite rules into a separate file, eg. rewrite.conf, and include it like so

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

<Directory /srv/project/sites/admin.project.hu/htdocs/>
  Include rewrite.conf
</Directory>
like image 128
ax. Avatar answered Jan 29 '26 12:01

ax.


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

That seems fairly clean to me...

like image 41
David Z Avatar answered Jan 29 '26 12:01

David Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!