views:

385

answers:

2

What I'm looking to do is basically take all requests to a directory, and if the file exists, send it. If not, send it from the parent directory (assume it exists there). The files are large and there can be a lot, and the subdirectories will change frequently, so filesystem links isn't a great way to manage. Is there some Apache config way of doing this? e.g.

/path/file0
/path/file1
/path/sub1/fileA
/path/sub1/fileB
/path/sub1/fileC
/path/sub2/fileA
/path/sub2/fileB
/path/sub2/fileC

So, if a request comes in for /path/sub1/fileB they get /path/sub1/fileB (normal-case). If a request comes in for /path/sub1/file0 they get /path/file0 (special-case).

Or maybe there's a way in PHP, if I could have Apache redirect all requests in one folder to a specific php file that checks if the file is present and if not checks 'up' a folder?

Thanks.

A: 

Yes, PHP can redirect to a parent directory.

mcandre
that could work, if somehow i could get apache to send all requests to the php file that set the header... then it could basically check if the file was present, and if so send it, and if not send the redirect header... or even better send the file from another location. so i guess i could use Gumbo's rule above to just re-write everything to the one php file...
+1  A: 

You could use mod_rewrite to do that:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^path/[^/]+/([^/]+)$ path/$1 [L]

This rule will rewrite a request of /path/foo/bar to /path/bar only if /path/foo/bar is not a regular file.

Gumbo
thank you, this seems to either be it, or be close. does this end up sending a 302 moved to that location, or does it actually send the file from that location as if it were in the current path? my preference is the latter behavior... i'll try it out...
This will cause an internal redirect. So the client will not notice any difference whether the file actually exists or not.
Gumbo