Mobile app version of vmapp.org
Login or Join
Miguel251

: Can Apache serve resources recursively if there's a 404 in the current directory? Is there a possibility to serve resources hosted with Apache recursively across the directory hierarchy, using

@Miguel251

Posted in: #Apache #ModRewrite

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 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 example.com/sites/site[1..3]/, all pages refer to header.png in the current directory. For sites 1 and 2 example.com/sites/site[1 2]/header.png does not exist, but example.com/sites/header.png does - so serve this in place. For site 3 example.com/sites/site3/header.png exists, so Apache serves it as usual, ignoring header.png further up in the tree.

However, if example.com/sites/header.png does not exist, also check 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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Miguel251

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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

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


If the requested file does not exist (!-f) then internally rewrite the request by removing the parent directory ((?:[^/]+)) from the URL. is a backreference to the part of the URL-path before the parent directory, ending in a slash (if any). And 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...


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


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


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme