4

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.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Andy Swift
  • 2,179
  • 3
  • 32
  • 53

6 Answers6

3

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.

Andy Swift
  • 2,179
  • 3
  • 32
  • 53
  • 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 Nov 03 '09 at 18:16
  • That is easily solved within program.php in the style of a "&sendHeader=404" setting. – Pekka Nov 03 '09 at 18:43
  • Note that this solution isn’t logged as a 404 by your webserver. – Gumbo Nov 03 '09 at 21:14
  • 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. – Andy Swift Nov 04 '09 at 07:30
2

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
  • 45,033
  • 10
  • 85
  • 120
  • 1
    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. – Andy Swift Nov 03 '09 at 17:35
0

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
  • 442,112
  • 142
  • 972
  • 1,088
0

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
  • 27,203
  • 20
  • 110
  • 132
0

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
  • 442,112
  • 142
  • 972
  • 1,088
  • 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. – Andy Swift Nov 03 '09 at 20:08
0

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
  • 442,112
  • 142
  • 972
  • 1,088