views:

172

answers:

6

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 prints 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.

A: 

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
Pekka
See my comment for the other answer.
Andrew Swift
+1  A: 

You want:

ErrorDocument 404 /program.php

According to the docs, "URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot)."

Seth
Using /program.php won't work because the program is in /somefolder/program.php, and I can't predict the name of "somefolder". The .htaccess file is in "somefolder" along with program.php.
Andrew Swift
A: 

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.

rjmunro
+1  A: 

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.

Andrew Swift
The problem with this is that it will not return a 404 status code, so search engines and link checker tools etc. will not know that they have reached a page that shouldn't exist.
rjmunro
Pekka
Note that this solution isn’t logged as a 404 by your webserver.
Gumbo
In fact, that is the preferred action. I will be creating thumbnail images on the fly, and I want it to show up correctly the first time even though it will be a php script creating the image.
Andrew Swift
A: 

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.

Pekka
The problem is that this is a program that I want to use in lots of places, and I don't want to modify the structure of the whole site each time. I really want to get it to work in an isolated folder, without affecting what's outside.
Andrew Swift
A: 

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?

Pekka