Talk #1: Reuse Apache rules in multiple directories or websites

“Talk” is a small series about what I learned today (well, sometimes it will be.). It contains small hints, ideas, tutorials or thought on Java and php or cs and programming in general.

This part of the series will cover how to configure multiple directories or websites in apache at once using the same rules.

Reuse Apache config rules for different directories / websites

It is relatively easy to reuse parts of an apache config file using the Include keyword.

What is important:
If you want to include some rule in a directory, the file that is included must be outside of the apache sites-enabled directory as all files in that directory are auto-included and rules such as AllowOverride need to be inside a directory tag.
If you think about it, it makes sense, but I was stumped there for a little while and that is why I created this post.

Example: same Apache config rules for multiple directories

# /etc/apache2/sites-enabled/somesite.conf
<VirtualHost *:80>
    # [... general settings ...]
    <Directory /var/www/some/directory/www/>
        Include configs/allow.conf
        # [... more rules ...]

    <Directory /var/www/some/directory/www/something/special>
        Include configs/allow.conf
        # [... different rules ...]

    <Directory /var/www/some/other/dir/www/>
        Include configs/allow.conf
        # [... more rules ...]

<VirtualHost *:80>
# /etc/apache2/configs/allow.conf
# some apache configurations that are used in multiple locations
Options -Indexes FollowSymLinks    
AllowOverride None
Order allow,deny
allow from all

Note that configs/allow.conf is outside the sites-enabled directory.

Leave a Reply

Your email address will not be published.