Let me preface this by saying that I admittedly do NOT administer Apache web servers. I typically only "use" Mod_Rewrite once a year or so, but have never tried to configure Apache and Mod_Rewrite before other than to use the default settings in XAMPP so that I could write some basic PHP web pages. So I have only a VERY cursory knowledge of how to configure Apache. But I have an issue that I am finding difficult to resolve. That being said...
I am trying to configure Apache and Mod_Rewrite under a recent version of XAMPP so that I can write and test redirect/rewrite rules on my local Windows machine. However, I've noticed that Apache 2.4.10 works very differently than older versions and/or other configurations that I have used before.
Previously, when I had the following setup:
/.htaccess
/category/.htaccess
/category/subcat/.htaccess
and I requested /category/subcat/some-file.ext, Mod_Rewrite would essentially concatenate the rules from all .htaccess files that existed in the various folders in the requested URL path and evaluate them in reverse order li:
/category/subcat/.htaccess
/category/.htaccess
/.htaccess
More specifically, older versions seemed to evaluate the rules as follows:
- Evaluate the RewriteRule directives in /category/subcat/.htaccess first. It would match patterns in the RewriteRule directives against some-file.ext (i.e. it removed the portion of the path down to and including the /category/subcat/ directory where the .htaccess file being evaluated exists). If a match is found and the [L] flag is included in the RewriteRule, it would break out and do whatever the matched rule says (e.g. if [R=301,L] then it would break out of rule processing and 301 redirect to the target URL.)
- If no match was found in /category/subcat/.htaccess it would evaluate the RewriteRule directives from the /category/.htaccess next. It would match patterns in those RewriteRule directives against subcat/some-file.ext (i.e. it removed the portion of the path down to and including the /category/ directory where the .htaccess file being evaluated exists). If a match is found and the [L] flag is included in the RewriteRule, it would break out and do whatever the matched rule says (e.g. if [R=301,L] then it would break out of rule processing and 301 redirect to the target URL.
- If no match was found in /category/.htaccess it would evaluate the RewriteRule directives from the /.htaccess next. It would match patterns in those RewriteRule directives against category/subcat/some-file.ext (i.e. it removed the portion of the path down to and including the root directory where the .htaccess file being evaluated exists). If a match is found and the [L] flag is included in the RewriteRule, it would break out and do whatever the matched rule says (e.g. if [R=301,L] then it would break out of rule processing and 301 redirect to the target URL.
So if I wanted to redirect /category/subcat/some-file.ext I had choices.
- I could put a rule in /.htaccess like RewriteRule ^category/subcat/some-file\.ext$ http://example.com/other-file.ext [R=301,L,NC] or
- I could put a rule in /category/.htaccess like RewriteRule ^subcat/some-file\.ext$ http://example.com/other-file.ext [R=301,L,NC] or
- I could put a rule in /category/subcat/.htaccess like RewriteRule ^some-file\.ext$ http://example.com/other-file.ext [R=301,L,NC]
Initially, what I was seeing under XAMPP and Apache 2.4.10 when I requested localhost/category/subcat/some-file.ext was that it ONLY executed rules in the /category/subcat/.htaccess. If no RewriteRule directives were matched to some-file.ext then rule processing stopped. It would never evaluate rules in the /category/.htaccess or /.htaccess files.
I discovered that this was because by default, under Apache 2.4 Mod_Rewrite does not "inherit" rules from parent folders. So I added a
RewriteOptions Inherit directive to all of my .htaccess files.
The addition of
RewriteOptions Inherit to all .htaccess files did trigger the inheriting of rules from additional .htaccess files in parent, grandparent, etc. folders, respectively, should all of the RewriteRules in the .htaccess file where the requested page "lives" fail. However, the behavior still seems to be different than in previous versions.
By this I mean that the patterns in all RewriteRules (regardless of whether the RewriteRule was in the .htaccess file where the requested page "lives" or in an inherited .htaccess file from an ancestor folder) seem to always be compared to "
some-file.ext" rather than the rules from /category/subcat/.htaccess being matched against
some-file.ext and rules from /category/.htaccess being matched against
subcat/some-file.ext and rules from /.htaccess being matched against
category/subcat/some-file.ext.
So I have a couple of questions:
Is there a way such that when
RewriteOptions Inherit in being used and I request
localhost/category/subcat/some-file.ext to force the RewriteRule directives in /category/subcat/.htaccess to match against
some-file.ext and if no matches then for RewriteRule directives in /category/.htaccess to match against
subcat/some-file.ext and if no matches then for RewriteRule directives in /.htaccess to match against
category/subcat/some-file.ext?
And also, is there a place in the httpd.conf where I can place
RewriteOptions Inherit once and have it apply across the enter server or to a given VirtualHost?
Thanks in advance!