I want to take requests for missing files in a specific folder and redirect them to program.php in that folder.
This is a program I will use a lot in different places, and I would prefer to avoid specifying the name of the folder in the htaccess file.
I have tried just putting:
errordocument 404 program.php
but the result is that the browser just pri开发者_如何学编程nts out "program.php" and doesn't execute program.php.
I understand why this is happening: based on the documentation at http://httpd.apache.org/docs/2.0/mod/core.html, url's that don't begin with a slash or http are interpreted as a text message.
The question is how to get around this limitation to be able to access the same folder.
I ended up using rewriterule instead of errordocument:
rewriteengine on rewritecond %{request_filename} !-f rewriterule ^(.+).jpg$ program.php?i=$1.jpg [L]
The second line verifies that the file does not exist, so that existing images will be shown correctly.
You want:
ErrorDocument 404 /program.php
According to the docs, "URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot)."
Have you tried a (relative or absolute) path? If you make it relative, it should be to the web root, which might server your purpose.
ErrorDocument 404 /program.php
If you have only a small number of folders, you can probably use:
<Directory /path/a>
ErrorDocument 404 /a/program.php
</Directory>
<Directory /path/b>
ErrorDocument 404 /b/program.php
</Directory>
<Directory /path/c>
ErrorDocument 404 /c/program.php
</Directory>
If that is impractical, then you should only have one program.php in the root and have it respond differently depending on the contents of the REDIRECT_URL environment variable, which is $_SERVER['REDIRECT_URL'] in php.
Why not have one ErrorDocument handle all the errors in all the folders? Might be much cleaner that way. You could then handle the individual case using $_SERVER["REQUEST_URI"] which should contain the original request if I remember correctly.
How about this. In your root path, you set up a generic error.php file. However, all it does is parse the REQUEST_URI, check the path, see whether there is a custom error.php there, and include that. Could work, huh?
精彩评论