Home subdirectory htaccess file breaks RewriteRule in parent htaccess
Reply: 0

subdirectory htaccess file breaks RewriteRule in parent htaccess

Hristiyan Dodov
1#
Hristiyan Dodov Published in 2018-01-11 13:31:36Z

I have the following directory structure:

root
    _COMMON
        jquery.min.js
    project
        index.html
        .htaccess
    project2
        index.html
        .htaccess
    .htaccess

In my project and project2, I use files from _COMMON. However, I do a lot of rewriting in those project folders and the references to _COMMON vary. They may look like:

  • http://example.com/project/_COMMON/jquery.min.js
  • http://example.com/project2/foobar/_COMMON/jquery.min.js
  • http://example.com/project/lorem/ipsum/243096/sit/amet/_COMMON/jquery.min.js

No matter where in the URL, if _COMMON is present, I always want it to point to the _COMMON folder in the root.

To do that, I put the following in my root/.htaccess:

RewriteEngine On
RewriteRule /_COMMON/(.*) ./_COMMON/$1 [L]

Then, if I navigate to http://example.com/project/test/bar/23/_COMMON/jquery.min.js I get the jQuery file I'm looking for.

However...

This only works if I have nothing in the subdirectory (project) htaccess file - if it doesn’t do anything.

As far as I know, this happens because options from parent htaccess files are ignored by default. I put the following in root/project/.htaccess so that they are applied:

RewriteEngine On
RewriteOptions InheritBefore

They actually run, but the rule rewrites to ./_COMMON/$1 and ./ means the current directory, which is now going to be project, instead of root. In the end, I get project/_COMMON/... instead of root/_COMMON/....


To better illustrate the problem, I requested the following URL: http://example.com/project/test/bar/23/_COMMON/jquery.min.js

If I do not have a root/project/.htaccess, it works and here are the logs:

add path info postfix: http://example.com/project/test -> http://example.com/project/test/bar/23/_COMMON/jquery.min.js
strip per-dir prefix: http://example.com/project/test/bar/23/_COMMON/jquery.min.js -> project/test/bar/23/_COMMON/jquery.min.js
applying pattern '/_COMMON/(.*)' to uri 'project/test/bar/23/_COMMON/jquery.min.js'
rewrite 'project/test/bar/23/_COMMON/jquery.min.js' -> './_COMMON/jquery.min.js'
add per-dir prefix: ./_COMMON/jquery.min.js -> http://example.com/./_COMMON/jquery.min.js
strip document_root prefix: http://example.com/./_COMMON/jquery.min.js -> /adiko-repo/./_COMMON/jquery.min.js
internal redirect with /adiko-repo/./_COMMON/jquery.min.js [INTERNAL REDIRECT]
strip per-dir prefix: http://example.com/_COMMON/jquery.min.js -> _COMMON/jquery.min.js
applying pattern '/_COMMON/(.*)' to uri '_COMMON/jquery.min.js'
pass through http://example.com/_COMMON/jquery.min.js

Final URL is: http://example.com/_COMMON/jquery.min.js (correct)

If I do have the htaccess file, it doesn’t work and I get:

add path info postfix: http://example.com/project/test -> http://example.com/project/test/bar/23/_COMMON/jquery.min.js
strip per-dir prefix: http://example.com/project/test/bar/23/_COMMON/jquery.min.js -> test/bar/23/_COMMON/jquery.min.js
applying pattern '/_COMMON/(.*)' to uri 'test/bar/23/_COMMON/jquery.min.js'
rewrite 'test/bar/23/_COMMON/jquery.min.js' -> './_COMMON/jquery.min.js'
add per-dir prefix: ./_COMMON/jquery.min.js -> http://example.com/project/./_COMMON/jquery.min.js
strip document_root prefix: http://example.com/project/./_COMMON/jquery.min.js -> /adiko-repo/project/./_COMMON/jquery.min.js
internal redirect with /adiko-repo/project/./_COMMON/jquery.min.js [INTERNAL REDIRECT]
add path info postfix: http://example.com/project/_COMMON -> http://example.com/project/_COMMON/jquery.min.js
strip per-dir prefix: http://example.com/project/_COMMON/jquery.min.js -> _COMMON/jquery.min.js
applying pattern '/_COMMON/(.*)' to uri '_COMMON/jquery.min.js'
pass through http://example.com/project/_COMMON

Final URL is: http://example.com/project/_COMMON (incorrect)


Question:

How do I implement this behavior? I want to get files from root/_COMMON if _COMMON appears anywhere in the URL. I need to make it work on Apache 2.2.22.


Note: I replaced the actual path to my root folder in the logs with http://example.com/ for simplicity.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.419467 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO