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.

  • Can someone who knows Apache, please fix the title to make sense? 404 != redirect. – John Saunders Aug 03 '09 at 15:41
  • Correct, 404 is "not found"... in the case of "not found", I want to send the file from some other location. –  Aug 03 '09 at 16:50

2 Answers2

3

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
  • 643,351
  • 109
  • 780
  • 844
  • 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... –  Aug 03 '09 at 17:26
  • This will cause an internal redirect. So the client will not notice any difference whether the file actually exists or not. – Gumbo Aug 03 '09 at 17:41
0

Yes, PHP can redirect to a parent directory.

mcandre
  • 22,868
  • 20
  • 88
  • 147
  • 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... –  Aug 03 '09 at 17:29