3

Is there a possibility to serve resources hosted with Apache recursively across the directory hierarchy, using mod_rewrite or the like?

Example#1:

A server document css/php/html/... refers to the resource image.png at /doc/root/dir1/dir2/dir3/image.png (requested with http://example.com/dir1/dir2/dir3/image.png) in the current directory. Apache serves the requested document but looks at dir2/ if it 404s in dir3/, then at dir1/ if it 404s in dir2/, etc...

I am trying to recreate Zope's ability to serve content that is higher up in the directory tree if it isn't available at the current path.

The idea is to create satellite pages further down the tree that make use of content remaining the same across all pages but that can be overridden if need be.

Example#2:

3 sites under the same domain at http://example.com/sites/site[1..3]/, all pages refer to header.png in the current directory. For sites 1 and 2 http://example.com/sites/site[1|2]/header.png does not exist, but http://example.com/sites/header.png does - so serve this in place. For site 3 http://example.com/sites/site3/header.png exists, so Apache serves it as usual, ignoring header.png further up in the tree.

However, if http://example.com/sites/header.png does not exist, also check http://example.com/header.png and serve it in place if it exists. Only return a 404 if this doesn't exist either.

Caveat: header.png is just an example and it should work for all resources, be they CSS, PHP or other content and as far down the directory tree as DocumentRoot.

Can this be done, and if yes, how?

2
  • Yes, I think this can be done. Although the thought occurs that this should perhaps be handled by the application, rather than Apache? Also, "it should work for all resources" - you should be specific about which resources this should cover. Presumably you don't literally mean everything?
    – MrWhite
    Jun 3 '16 at 10:33
  • @w3dk Thanks for the application handling tip but since the logic is more dynamic on the content side than the structural/ interactional, i'd like apache to handle it and test performance once in place. As to the resource types: why restrict? Why not allow all mime-types apache knows about and handle special files (hidden/restricted etc) as per usual? In my case, I'm looking esp. for css, html, php, png, jpg and the odd audio or video file.
    – MaKaDev
    Jun 3 '16 at 10:43
1

You could try the following (mod_rewrite directives) in the .htaccess file in the document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+/)?(?:[^/]+)/([^/]+)$ $1$2 [L]

If the requested file does not exist (!-f) then internally rewrite the request by removing the parent directory ((?:[^/]+)) from the URL. $1 is a backreference to the part of the URL-path before the parent directory, ending in a slash (if any). And $2 is a backreference to just the filename (less the slash prefix).

This applies to every request, except to requests in the document root (since the RewriteRule pattern will fail to match in this instance).

For example, only /dir1/image.png exists, then...

  1. Request: http://example.com/dir1/dir2/dir3/image.png (does not exist)
  2. Internal rewrite to: http://example.com/dir1/dir2/image.png (does not exist)
  3. Internal rewrite to: http://example.com/dir1/image.png (Success!)

For example, if image.png did not exist at all, then...

  1. Request: http://example.com/dir1/dir2/dir3/image.png (does not exist)
  2. Internal rewrite to: http://example.com/dir1/dir2/image.png (does not exist)
  3. Internal rewrite to: http://example.com/dir1/image.png (does not exist)
  4. Internal rewrite to: http://example.com/image.png - 404 NOT FOUND

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.