Bug 59921 - RewriteRule cannot be made to inherit the matching Directory directive rules in .htaccess files
Summary: RewriteRule cannot be made to inherit the matching Directory directive rules ...
Status: NEW
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: Core (show other bugs)
Version: 2.4.23
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-01 11:44 UTC by Viktor Szépe
Modified: 2016-08-05 15:28 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Viktor Szépe 2016-08-01 11:44:55 UTC
Core doc says:

If multiple (non-regular expression) <Directory> sections match the directory (or one of its parents) containing a document, then the directives are applied in the order of shortest match first, interspersed with the directives from the .htaccess files.

I have two Directory directives with the same path (in realty the second one is in an Include that is used by all vhost configs)


Request URI:
http://subtwo.wp/blog/

My config:
    DocumentRoot "/var/www/subtwo/2"
    <Directory "/var/www/subtwo/2">
        # Core permalinks
        RewriteRule "^index\.php$" - [END]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule "^" "index.php" [END]
    </Directory>

    <Directory "/var/www/subtwo/2">
        RewriteRule "^blog/$" "/some-url"
    </Directory>

End up in /some-url:
init rewrite engine with requested uri /blog/
pass through /blog/
[perdir /var/www/subtwo/2/] add path info postfix: /var/www/subtwo/2/blog -> /var/www/subtwo/2/blog/
[perdir /var/www/subtwo/2/] strip per-dir prefix: /var/www/subtwo/2/blog/ -> blog/
[perdir /var/www/subtwo/2/] applying pattern '^blog/$' to uri 'blog/'
[perdir /var/www/subtwo/2/] rewrite 'blog/' -> '/some-url'
[perdir /var/www/subtwo/2/] trying to replace context docroot /var/www/subtwo/2 with context prefix
[perdir /var/www/subtwo/2/] internal redirect with /some-url [INTERNAL REDIRECT]

/redir#1] init rewrite engine with requested uri /some-url
/redir#1] pass through /some-url
/redir#1] [perdir /var/www/subtwo/2/] strip per-dir prefix: /var/www/subtwo/2/some-url -> some-url
/redir#1] [perdir /var/www/subtwo/2/] applying pattern '^blog/$' to uri 'some-url'
/redir#1] [perdir /var/www/subtwo/2/] pass through /var/www/subtwo/2/some-url


The other way around:

Config:
    DocumentRoot "/var/www/subtwo/2"
    <Directory "/var/www/subtwo/2">
        RewriteRule "^blog/$" "/some-url"
    </Directory>

    <Directory "/var/www/subtwo/2">
        # Core permalinks
        RewriteRule "^index\.php$" - [END]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule "^" "index.php" [END]
    </Directory>

Ends up in /index.php:
init rewrite engine with requested uri /blog/
pass through /blog/
[perdir /var/www/subtwo/2/] add path info postfix: /var/www/subtwo/2/blog -> /var/www/subtwo/2/blog/
[perdir /var/www/subtwo/2/] strip per-dir prefix: /var/www/subtwo/2/blog/ -> blog/
[perdir /var/www/subtwo/2/] applying pattern '^index\\.php$' to uri 'blog/'
[perdir /var/www/subtwo/2/] add path info postfix: /var/www/subtwo/2/blog -> /var/www/subtwo/2/blog/
[perdir /var/www/subtwo/2/] strip per-dir prefix: /var/www/subtwo/2/blog/ -> blog/
[perdir /var/www/subtwo/2/] applying pattern '^' to uri 'blog/'
[perdir /var/www/subtwo/2/] RewriteCond: input='/var/www/subtwo/2/blog' pattern='!-f' => matched
[perdir /var/www/subtwo/2/] RewriteCond: input='/var/www/subtwo/2/blog' pattern='!-d' => matched
[perdir /var/www/subtwo/2/] rewrite 'blog/' -> 'index.php'
[perdir /var/www/subtwo/2/] add per-dir prefix: index.php -> /var/www/subtwo/2/index.php
[perdir /var/www/subtwo/2/] strip document_root prefix: /var/www/subtwo/2/index.php -> /index.php
[perdir /var/www/subtwo/2/] internal redirect with /index.php [INTERNAL REDIRECT]


Is this behavior just undocumented or buggy?
Thans.
Comment 1 Viktor Szépe 2016-08-01 12:05:46 UTC
https://httpd.apache.org/docs/2.4/sections.html also says:

If multiple <Directory> sections apply to the same directory they are processed in the configuration file order.
Comment 2 Eric Covener 2016-08-01 12:44:20 UTC
Does RewriteOptions Inherit change the behavior?
Comment 3 Viktor Szépe 2016-08-01 18:01:08 UTC
(In reply to Eric Covener from comment #2)
> Does RewriteOptions Inherit change the behavior?

Thank you very much for your comment.
It solved the two Directory directives problem.

I've noticed that the .htaccess in the same directory overrides the rewite rules.
I've tried placing RewriteOptions Inherit into the .htaccess file at several places with no success.
Could it be solved somehow?
Comment 4 Eric Covener 2016-08-01 18:02:59 UTC
(In reply to Szépe Viktor from comment #3)
> (In reply to Eric Covener from comment #2)
> > Does RewriteOptions Inherit change the behavior?
> 
> Thank you very much for your comment.
> It solved the two Directory directives problem.
> 
> I've noticed that the .htaccess in the same directory overrides the rewite
> rules.
> I've tried placing RewriteOptions Inherit into the .htaccess file at several
> places with no success.
> Could it be solved somehow?

I would have thought it would behave the same -- the htaccess rules replace the matching <Directory> rules unless you tell it to inherit in that rewrite-specific way.
Comment 5 Viktor Szépe 2016-08-01 18:56:38 UTC
> I would have thought it would behave the same -- the htaccess rules replace
> the matching <Directory> rules unless you tell it to inherit in that
> rewrite-specific way.

Thank you again.

A temporary word-aroung could be to use the DirectoryMatch directive as it will surely override directory rules.
http://httpd.apache.org/docs/2.4/sections.html#merging
Comment 6 Viktor Szépe 2016-08-05 15:28:18 UTC
A strange (for me at least) fact is:

Rewrite order is:

DirectoryMatch -> .htaccess -> Directory

But if any of these matches - regardless of [L] or [END] - rewrite processing stops there.