Markus AO Markus AO - 14 days ago 7
Apache Configuration Question

Apache httpd.conf mod_rewrite canceled if .htaccess uses mod_rewrite

Darn it took a while tracking this quirk down. I have global rewrite-rules defined in

httpd.conf
(or in an include; won't make a difference), to force an external redirect for certain URL patterns:

<Directory "/home/*">
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*wrong\.php$ http://example.net/void.html [R,L]
</IfModule>
</Directory>


This works brilliant for any virtual host that doesn't have a
.htaccess
file using mod_rewrite. However, if a vhost uses any mod_rewrite rules at all, the above is not working. Even a mere
RewriteEngine on
, without any rules specified, is enough to cancel out the
httpd.conf
rules for the vhost. Is this expected behavior? On Apache/2.2.15. I've tried with and without R & L flags, with and without a
<Directory>
wrapping, and declared before or after virtual hosts, but the rules simply won't work.

Someone save my sanity and tell me how to have (non-conflicting) rules in
httpd.conf
and
.htaccess
working together. It must be possible, and I'd expect it to be the default behavior?

Edit: As requested, a sample
.htaccess
file: =^_^=

RewriteEngine on


Even just that and that only. Enough to make the rules not match, seems like any earlier
mod_rewrite
gets wiped out and a new clean rule set started when
.htaccess
kicks in. Also, any other line having to do with
mod_rewrite
causes the same. If I stick into the
.htaccess
e.g.
RewriteRule ^([X-Z])heRmaGerd[1-3]\.FOObAR$ /wherever/$1
(with the
RewriteEngine on
, or by itself as the only line), it will surely not match, but is enough cancel out all that was defined before.

Answer

It looks like (at least on 2.4.6) that RewriteOptions inherit helps, placed either in httpd.conf or .htaccess.

According to docs, this should only force VirtualHost to inherit rules from main server configuration, or in per-directory context to inherit parent directory's .htaccess configuration, but I tested and it seems to resolve your issue.

Also:

Rules inherited from the parent scope are applied after rules specified in the child scope.