Tutorial :Same rules for multiple directories in Apache?



Question:

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.


Solution:1

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>  


Solution:2

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

That seems fairly clean to me...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »