How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

hourback picture hourback · Dec 5, 2008 · Viewed 20.4k times · Source

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I do this?

I found this but my question is the same: how can I do this without resorting to .htaccess files and performing some other action for each virtual host?

OMGTIA!

Answer

Matej Snoha picture Matej Snoha · Jun 29, 2015

Specify RewriteOptions InheritDown in the parent scope (such as httpd.conf) to get your rules applied in child Virtual Hosts without modifing them.

This will only work on Virtual Hosts where the RewriteEngine directive is set to on:

Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.

(source)

Apache supports this since 2.4.8 (not available at the time of the original question).

From documentation for RewriteOptions:

InheritDown

If this option is enabled, all child configurations will inherit the configuration of the current configuration. It is equivalent to specifying RewriteOptions Inherit in all child configurations. See the Inherit option for more details on how the parent-child relationships are handled. Available in Apache HTTP Server 2.4.8 and later.

InheritDownBefore

Like InheritDown above, but the rules from the current scope are applied before rules specified in any child's scope. Available in Apache HTTP Server 2.4.8 and later.

IgnoreInherit

This option forces the current and child configurations to ignore all rules that would be inherited from a parent specifying InheritDown or InheritDownBefore. Available in Apache HTTP Server 2.4.8 and later.

(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions)