Welcome to WebmasterWorld Guest from 35.175.174.157

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Parsing PHP with HTML file extension

clarification on earlier posts

     
9:33 pm on Apr 2, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


I know this issue has been discussed here before, but I'm having problems making this work and I wonder if anyone can tell me why.

I thought I could use an htaccess file to tell the server to parse php in any file with either a .php or .html extension using just this line:

AddType application/x-httpd-php .php .html

This does not work (php was not parsed at all), although my host says I should be able to use htaccess to do it (they don't give support for the actual code).

Am I missing something else? Does anything else need to be done?

Any help would be greatly appreciated!

10:19 pm on Apr 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2003
posts:2355
votes: 0


Check out:

[webmasterworld.com...]
Regards,
Brent

[edited by: WebGuerrilla at 11:13 pm (utc) on April 3, 2003]

[edited by: coopster at 9:13 pm (utc) on Mar. 8, 2009]
[edit reason] fixed 404 link [/edit]

2:41 am on Apr 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Yes, that's the thread I was looking at and specifically this message from Birdman


my rewrite rule could be incorrect, but this line should allow you to use php in .html pages:

AddType application/x-httpd-php .php .php3 .phtml .html

Try adding just that line, then put a simple php echo statement on an .html page. Good luck!

I don't need to rewrite the URLs, and I understood from this message that I only need this one line in an htaccess file for php to work in html pages. Have I understood this correctly? If so, any ideas why it's not working?

3:12 am on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member chiyo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:June 21, 2000
posts:3170
votes: 0


It works for us. Indeed we only use it in a few directories that need php functionality in the pages. We dont know what an "echo statement" is, and we dont have one, but php "works" in all those directories.

My only suggestion is to make sure that the statement is in the actual "public" root directory in your space or server, if you use virtual hosting. Maybe you have it in the non-public directory, but not the public directory which has a different domain name?

5:28 am on Apr 3, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


Have you restarted apache afterwards? I believe that would be part of it but I could be wrong.

maybe look here Apache Tutorial: .htaccess files [httpd.apache.org]

5:42 am on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


Is that all your .htaccess file contains? If not, there could be an error earlier in the file.

Try to chmod the file... Maybe that helps.

1:01 pm on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


If you are using .htaccess files there is no need to restart the server since they are read anew for each and every request.

Did yu test whether Apache [httpd.apache.org] really reads the htaccess file? A simple way is to add some garbage which will then cause an Internal Server Error.

Andreas

6:35 pm on Apr 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Still stuck. The htaccess file I'm now using is just this one line

AddType application/x-httpd-php .php .html

PHP (4.2.3) is running in cgi mode on this server. I moved the htaccess file up one directory to see if that helps, it didn't. I know the server is reading it because other tests with addl info returned errors. I thought perhaps I needed to tell the server where to find php so I tried adding the line

Action application/x-httpd-php /cgi-bin/php.exe

(this is on a unix server) I don't really know what extension php should have in this case so I tried several-none worked (though one of them looked for the index file in the cgi-bin).

I appreciate all the comments, which have encouraged me to keep at this, thinking I might be able to get it to work, but no luck so far. Does the fact php is running in cgi mode affect the way the htaccess file would work?

Perhaps you can tell I'm not a wiz at this ;=), but I really would like to figure it out for this and future needs. Thanks

6:42 pm on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


>>PHP [php.net] (4.2.3) is running in cgi mode on this server

Now thatīs the key phrase here.


AddType [httpd.apache.org] application/x-httpd-PHP [php.net] .PHP [php.net] .html

will only work for the PHP [php.net] module. For the CGI version you need to tell Apache [httpd.apache.org] what files are CGI scripts:


AddHandler [httpd.apache.org] cgi-script cgi PHP [php.net] html

Then add a line like this to the beginning of your PHP [php.net] CGI scripts:


#!/usr/local/bin/PHP [php.net]

You need to adjust the path to your PHP [php.net] binary. Use lower case PHP [php.net] in your .htaccess file

Andreas

7:30 pm on Apr 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Hmm. Since php files with the .php extension are being executed correctly, do I still need the AddHandler line?

All I'm trying to do is have the server parse php when it's in a file with the .html extension. Everything else is working.

Moments ago I found this item in comments to the PHP manual

Finally figured this out: if you are using server side includes (SSI) with Apache, and have added a line

AddHandler server-parsed .html

in your httpd.conf file so that .html files can use SSI, this setting will prevent

AddType application/x-httpd-php .php .html

from working (.php will work, but .html won't). From what I've figured out so far, you need to dump SSI support for .html files if you want to get PHP to work with .html files.

Since I'm working in a shared hosting environment, I don't have access to the httpd.conf file. I tried RemoveHandler .html in the htaccess but it didn't work. I tried adding your AddHandler line and got a server error.

7:58 pm on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0



>>server error

Whatīs in the error log?

>>Since PHP [php.net] files with the .PHP [php.net] extension are being
>>executed correctly, do I still need the AddHandler line?

You do, since so far Apache [httpd.apache.org] only treat files with a PHP [php.net] extension as CGI files that are to be handled by mod_cgi [httpd.apache.org].

>>All I'm trying to do is have the server parse PHP [php.net] when
>>it's in a file with the .html extension. Everything else
>>is working.

Thatīs why you need to tell Apache [httpd.apache.org] that files with a html extension are to be handled by mod_cgi [httpd.apache.org] as well.

Andreas

8:40 pm on Apr 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Thanks for the explanation - makes perfect sense.

When I used the following lines in an htaccess file:

AdddHandler cgi-script cgi php html
AddType application/x-httpd-php .php .html

I got a 500 server error.

I'm not sure how or where to modify the path to the php binary. If you're talking about the main files in the cgi-bin, I don't have permission to alter those. I can, however, write to the php.ini file. Does that help?

8:57 pm on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


If you change the spelling to "AddHandler" it might work better ;)
11:05 pm on Apr 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Well I did fix that typo <blush> and tried again, but no go. I contacted the host again, pleading for more support, and just heard back from them that with SSI enabled and PHP running in cgi mode, htaccess won't work to parse html files. I pressed them on whether they really knew it was impossible since they had earlier told me it should work. This is what they said:

Yes, it was confirmed by our PHP specialist that SSI enabling is causing the problem. We also tested this using 2 external servers outside our network, one we disabled SSI and it worked and the second was a server with php loaded as an apache module with SSI enabled and it worked.

So we concluded that since PHP is loaded as a cgi and SSI is enabled then parsing PHP within an HTML file will not work.

So unless anybody knows of another way, I hereby give up. Thanks very much for the help. Maybe this thread will help others if they run into troubles.

11:44 pm on Apr 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


Sure Apache [httpd.apache.org] 1.x does not support stacked handlers. So a request will be handled either by mod_cgi [httpd.apache.org] or mod_include [httpd.apache.org].

Using AddHandler [httpd.apache.org] will override any mappings that already exist for the same extension.

.htaccess file


Options [httpd.apache.org] +ExecCGI +Includes
AddHandler [httpd.apache.org] server-parsed html
AddHandler [httpd.apache.org] cgi-script cgi PHP [php.net] html

.html file


#!/usr/bin/php4
<!--#echo var="DATE_LOCAL" -->
<?PHP [php.net] phpinfo [php.net]();?>

When I requested the html file it showed the PHP [php.net] info with a Server API of CGI, i.e. the script was indeed run by the PHP [php.net] CGI binary and not the PHP [php.net] module. After commenting out the second AddHandler [httpd.apache.org] directive it showed the local date, i.e. it was parsed by mod_include [httpd.apache.org].

Then I tested whether I could override the association of html file with mod_include [httpd.apache.org] from an .htaccess file when it was configured in httpd.conf.

httpd.conf file


AddHandler [httpd.apache.org] server-parsed html
<Directory [httpd.apache.org] "/where/htaccess/file/is/stored">
AllowOverride [httpd.apache.org] all
</Directory> [httpd.apache.org]

.htaccess file


Options [httpd.apache.org] +ExecCGI +Includes
AddHandler [httpd.apache.org] cgi-script cgi PHP [php.net] html

Requesting the html file showed the PHP [php.net] info page again. As expected you can override the associated handler as well. To test whether AddHandler [httpd.apache.org] server-parsed html really worked I commented out the AddHandler [httpd.apache.org] directive in the .htaccess file and requested the html again. It showed the date.

Either I am missing something or the support guys choose the easy way out.

Andreas

3:22 pm on Apr 4, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
posts:75
votes: 0


Andreas:

I tried using the information you gave and couldn't get it to work so I passed it on to the hosting people, asking them to please check again and see if this would help figure out how to use htaccess with their config. I heard back this morning that they tested some more and it would just not be possible to parse php within an html file due to the way their shared servers were configured. I really appreciate your help.