Enabling PHP in .html files

I need to have all .html files for a virtual domain (or the entire server if necessary) processed by the PHP parser (e.g., as if they were a .php file).

After a couple of hours of trying things with .htaccess, and adding MIME types, I'm coming up empty -- and it seems a common enough problem that there should be some documentation on it as well.

What is the best (e.g., blessed) way to have .html files processed by PHP before they are rendered?

Note: Fully updated for both OS and virtualmin patches to current versions.

Thanks!

Neil

Status: 
Active

Comments

That tends to be something that's recommended against for performance and security reasons, and I haven't actually tried it recently so I don't recall the steps off the top of my head.

However, what issue(s) are you running into when trying that?

If I were going to try that, the first place I'd start is to look at that domain's config file in /etc/apache2/sites-enabled/domain.com.conf -- you'll see some lines like this:

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

You could always try adding additional lines that looks like:

RemoveHandler .html .htm
AddType application/x-httpd-php5 .html .htm

Alternatively, I see another option mentioned that looks something like this:

<FilesMatch "\.html$">
    ForceType application/x-httpd-php
</FilesMatch>

Do either of those work for you?

Thanks ... unfortunately, neither work (I presume it was one or the other but not both).

The first one, when you go to a .html file then downloads the file rather than loads it into the web browser.

The second one seems to do nothing. The page loads in the browser, but it doesn't execute the php.

(Note: I test this using a file that I know the behavior [a redirect to another page] because when I copy the file and rename it with .php, the behavior works fine. The .html version has text in it that says "You should not be seeing this." if the php above hasn't taken us to another page. See http://static.mactech.com/search.html)

Thanks!

Neil

Okay, I did some digging for other Virtualmin users who got that working... it looks like this syntax did the trick:

RemoveHandler .html .htm
AddHandler fcgid-script .html
AddHandler fcgid-script .htm
FCGIWrapper /home/domain/fcgi-bin/php5.fcgi .html
FCGIWrapper /home/domain/fcgi-bin/php5.fcgi .htm

Try adding that to your Apache config, and let us know if that does the trick!

That was in this thread here:

https://www.virtualmin.com/node/7896

Unfortunately (and I read through that link before too) -- still doesn't work. In the interest of seeing if it's just something stupid, let me be more explicit...

There's two places that this is in the .conf -- one is :80 and one is :443 I put them in both, saved, and then restarted apache.

Between of these tags, and , I replaced this:

...
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .php5
</Directory>

with

...
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
 
# -- Added to handle .html files in php - by Neil NST 08-08-16
RemoveHandler .html .htm
AddHandler fcgid-script .html
AddHandler fcgid-script .htm
 
# -- these lines are original
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .php5
 
# -- Added to handle .html files in php - by Neil NST 08-08-16
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .html
FCGIWrapper /home/DOMAIN/fcgi-bin/php5.fcgi .htm
 
</Directory>

and I did that in both places (virtual hosts for ports 80 and 443). And then (again), restarted apache with:

/etc/init.d/apache2 restart

since it's Ubuntu 14.04 LTS.

See any issues?

I had some trouble getting this working using FCGID as the PHP Execution Mode, but upon switching it to CGI, I was able to get it working pretty quickly.

Using CGI, it works for me when the following is included in my VirtualHost section for the domain:

<Directory /home/test/public_html>
Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
RemoveHandler .html .htm
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html
AddType application/x-httpd-php5 .php5
AddType application/x-httpd-php5 .html
Action application/x-httpd-php5 /cgi-bin/php5.cgi

</Directory>

For completeness of answer, let's make sure this has a bit more documentation in case someone else needs it down the road.

For "upon switching it to CGI"

Virtualmin > Server Configuration > Website Options For PHP script execution mode Change from: FCGId to CGI wrapper

Then, there are TWO places that the above goes ... one for 80, one for 443. Once you are done editing those, don't forget to restart Apache (Ubuntu is /etc/init.d/apache2 restart). If there are lines in the config that duplicate the above, just comment them out with a "#" at the beginning of the line.

Thanks andreychek for figuring this out!