Search code examples
apache.htaccessmod-rewriteurl-rewritinghttpd.conf

Set a rewrite rule in httpd.conf which applies for all served sites


Ok, so I have a server which serves multiple websites.

www.site1.com
www.site2.com
...

I want to set up my rewriterule in httpd.conf (or another file I'll include in httpd.conf) so it applies for all websites at once, instead of having to set it up for every site seperately.

This doesn't work:

<Directory "${INSTALL_DIR}/www/">
    Options Includes FollowSymLinks
    AllowOverride none
    Require all granted
    RewriteRule ^/news/article/(.*)/(.*)$ news/article.php?id=$1
</Directory>

This works:

<Directory "${INSTALL_DIR}/www/site1/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=$1
</Directory>
<Directory "${INSTALL_DIR}/www/site2/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=$1
</Directory>

So basicly I probably have to add something Rewriterule [something here]/news/article/(.)/(.)$ news/article.php?id=$1 so it works from the www-dir for all websites.

Probably it's something stupid I'm missing... I already experimented a little, but I can't get it to work. Any help is much appreciated!

Edit: Here's the solution for future reference. In the vhosts file:

<Virtualhost site1>
...
        <Directory "${INSTALL_DIR}/www/site1">
            Include "${APACHE_DIR}/conf/extra/siterules.conf"
        </Directory>
...
</Virtualhost>
<Virtualhost site2>
...
        <Directory "${INSTALL_DIR}/www/site2">
            Include "${APACHE_DIR}/conf/extra/siterules.conf"
        </Directory>
...
</Virtualhost>

In siterules.conf:

RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=$1
and some other rules if you want.

And that's it :-) You change the rules in one place and it applies to all sites.

Much thx to @arkascha for pointing me in the right direction.


Solution

  • Typically separate sites are implemented using separate virtual hosts. If so, then I usually use a configuration file holding shared rules or directives which I include into all virtual host definitions. That allows to adjust the rules at a single location while they get applied to all including virtual hosts.

    If you do not operate different rules for different locations (note: locations, not hosts...) then things are even more simple: you can simply implement the rule outside a directory block.