5

This doesn't work:

ErrorDocument 401 ../../src/wrong_pwd.php

it renders the path as text, it really prints

../../src/wrong_pwd.php

and doesn't output the real file.

If I put the absolute path it will render the contents of /var/www/home (I don't really know why, maybe it's something in httpd.conf?)

How can I do that?

Andrea Ambu
  • 480
  • 1
  • 8
  • 13
  • This is a big limitation of ErrorDocument inside an .htacess, and there is no good substitute for handling HTTP errors in a subsite. Is it possible to use an expression for the current working directory? Example: "ErrorDocument, %{CURDIR}/error.php". Answer to my own question: no, there is no way to write a universal .htaccess that can be used for multiple subdirectories of a website. There is no way to specify an error handler in the current directory. – David Spector Dec 05 '22 at 11:31

2 Answers2

2

The docs say:

URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot), or be a full URL which the client can resolve. Alternatively, a message can be provided to be displayed by the browser.

../../src/wrong_pwd.php doesn't begin with a slash so I expect Apache treats it as a message. It looks like you can't return a document which is outside the DocumentRoot so I suspect what you want will never work (this is sensible; after all, the client has to be able to retrieve the document in case of an error).

You might be able to use Alias to alias the error page to a location outside the document root. Something like:

Alias /error/wrong_pwd.php /path/to/src/wrong_pwd.php
ErrorDocument 401 /error/wrong_pwd.php

Untested, YMMV, etc. You might need to explicitly grant access to this directory with a <Directory> block; see the mod_alias documentation. Of course, there are security concerns here -- generally you don't want to expose your source to the world. (If this is the case perhaps you could make a lightweight, web-visible PHP wrapper which include()s the /path/to/src/wrong_pwd.php...)

markdrayton
  • 2,449
  • 1
  • 20
  • 24
  • Yes I'm going to use a wrapper, the problem is I can't catch this error properly. Using alias gives an error on error.log: `Alias not allowed here`... – Andrea Ambu Aug 05 '09 at 09:02
  • Server error! Error message: ....htaccess Alias not allowed here – Pacerier Jun 07 '20 at 06:28
0

Have you tried a symlink?

ln -s ../../src/wrong_pwd.php src/wrong_pwd.php
Singletoned
  • 116
  • 2