AddHandler not working for .php files - Apache 2.4
Asked Answered
H

3

9

I am creating a dummy handler for .php and .html files called helloworld. The handler contains this:

static int helloworld_handler(request_rec *r){
    ap_rprintf(r, "hello world!");
    return OK;
}

I have got this in apache2.conf:

<Directory /var/www/html>
AddHandler helloworld .php .html
</Directory>

The handler "helloworld" is working for .html files, but it is not working for .php files. I think it is mostly because the default php handler overrides the helloworld handler for .php files. How do I make "helloworld" handler work for .php files?

If there is any extra information required please ask.

Hands answered 30/3, 2015 at 9:44 Comment(2)
Is this similar: serverfault.com/questions/593064/…Airing
Yes it is similar. But there is no correct answer there.Hands
C
6

You might want to try SetHandler instead

<FilesMatch \.php$>
    SetHandler helloworld
</FilesMatch>
Coworker answered 14/4, 2015 at 1:16 Comment(6)
This overrides the default php handler. If I do "return DECLINED" instead of "return OK" in helloworld_handler, the default php handler does not evaluate the php file. Can you please tell me how to not override the default php handler?Hands
I've never tried, but I don't see why you couldn't also add a second handler to send PHP files to application/x-httpd-php5, the default PHP handlerCoworker
I have added the edit section in the question. Could you help with it?Hands
yes I tried using SetHandler, however the php contents of the file does not get evaluated.Hands
I'm not sure this is still within the scope of the original question. The best thing I can suggest is to check out this thread which talks about loading multiple versions of PHPCoworker
The link you provided does not help.... my original question was to make a handler work along with php default handler. However, the default php handler is not evaluating the file after I plug in my handler. Is there any other thing I can do to make it work?Hands
S
0

SetHandler will remove any previously set handler. To get the option for your handler to run first then run the default PHP handler when your handler returns DECLINE you need to set yours first, then add PHP

<FilesMatch \.html$>
    SetHandler helloworld .html
</FilesMatch>
<FilesMatch \.php$>
    SetHandler helloworld .php
    AddHandler php5-script .php
</FilesMatch>

Note that this has the effect of clearing any handlers previously setup for .html files

You can only have one handler handle any file type per request. If you always want the PHP interpreter to run first and then your handler to run second you could consider AddOutputFilter directive.

Most phases are terminated by the first module that handles them; however, for logging, `fixups', and non-access authentication checking, all handlers always run (barring an error).

Apache API reference

AddOutputFilter docs

Savoy answered 19/4, 2015 at 23:22 Comment(0)
G
-3

You need to restart your Apache server after adding handler.

Check also if extension will actually be interpreted by the server the way you want it to work. You can check that sort of info from system admin of your hosting provider. Many sysadmins disable some features to improve security.

Goodkin answered 13/4, 2015 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.