0

I am trying to write a PHP extension that executes some operations for specific requests.

The problem is that, even if the handlers are set correctly for PHP to run, unless the request URL is mapped onto an existing file, it just displays a 'Not Found' error page, skipping the extension code.

Even though this might sound like a bogus usage of PHP, I really want PHP to run, regardless if a PHP script exist or not in the server.

Is there a directive I can use to suppress 'Not Found' error pages for a given regex match, without using redirects? If not, how could I achieve this using redirects?

ravemir
  • 1,153
  • 2
  • 13
  • 29

1 Answers1

1

Maybe you could use the ErrorDocument directive for that.
But then you'd have to handle all the 404 conditions. The originally requested path is somewhere in the $_SERVER array. So you could use your regex "within" the php script.

edit: the original path is stored in $_SERVER['REQUEST_URI'].
And the php script can send any http status code it wants, 404, 301, 200 whatever; although I don't know what happens with the new cascading definition thingy in apache 2.4 ...

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Problem is, there is no PHP script. The code I want to run is inside the PHP Extension, and I am pretty sure it is not running for requests that don't map to files on the server. – ravemir Aug 31 '12 at 10:51
  • But is it a problem to create a "dummy" php script? It can be otherwise inaccessible, i.e. you don't have to "expose" that script file - just add it to the ErrorDocument handler. – VolkerK Aug 31 '12 at 10:56
  • There is no problem to create one script, but when the URL can assume multiple forms, if I want my PHP extension to handle all of them, I had to create a dummy every time, so it is not an option. – ravemir Aug 31 '12 at 11:01
  • 1
    no, the errordocument 404 handler is invoked whenever a resource is not available. The "default" handler prints the standard 404 message, but if you put a php script in that place it gets executed and the original path is passed as a server environment parameter. i.e. one php script -> handler for all 404s – VolkerK Aug 31 '12 at 11:12
  • Hmm, I had discarded trying to use PHP scripts to do what I wanted within PHP, but I'll think I should reconsider – ravemir Aug 31 '12 at 11:18
  • It seems to be working fine. I was considering the [AliasMatch](http://httpd.apache.org/docs/2.2/mod/mod_alias.html#aliasmatch) directive, due to having regex matching capabilities. But if you encase the [ErrorDocument](https://httpd.apache.org/docs/2.2/mod/core.html#errordocument) directive inside a [LocationMatch](https://httpd.apache.org/docs/2.2/mod/core.html#locationmatch) directive, you can restrict the behavior to URL's matching that Location (I've tested it). May I edit your answer to include this info? – ravemir Aug 31 '12 at 11:31