PHP inside HTML files using .htaccess

Recently I was trying to fix a website that needed to run PHP script from within HTML files, the site originally used a htaccess rule to set up html files so they were executed just like php files but this had stopped working.

The problem is, there are many different methods for defining file extensions as php executables, and these depend on the server, the server software and how apache is setup.

So the question is, how can you easily figure out which rule is going to get your html files to run as php files? Here is the answer…

1) Login to the server in SSH and cd to /usr/local/apache/conf/:

cd /usr/local/apache/conf/

2) Then edit php.conf in your favourite editor, I like nano:

nano php.conf

3) Once the file is open locate the lines where php and etc. extensions are setup to execute with php, it will look a little like this:

AddType *** .php5 .php4 .php .php3 .php2 .phtml

or:

AddHandler *** .php5 .php4 .php .php3 .php2 .phtml

4) you can copy this line into your .htaccess file, removing all the paths and instead putting the new extensions you would like php to execute in, so for instance if the file showed this:

AddType application/x-httpd-php5 .php5 .php4 .php .php3 .php2 .phtml

Copy this into your .htaccess and modify it for htm/html files:

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

5) And you’ve done it!

 

Different Methods…

As I said, there are a number of different ways this can be achieved, making it confusing to figure out which way – the steps above will allow you to find the EXACT way your sever is already doing it, so you can mimic that method in your htaccess rules. However I appreciate some of you cannot access this file due to being on shared servers or etc. Below I have compiled a list of the different commands I have come across:

AddType application/x-httpd-php5 .html .htm
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php5 .html .htm
AddHandler application/x-httpd-php .html .htm
AddHandler x-httpd-php5 .html .htm
AddHandler x-httpd-php .html .htm
AddHandler fcgid-script .htm .html

 

PHP Inside HTML 1&1

1&1's hosting requires the following rules if you want to parse PHP inside HTML files (PHP 5.2):

AddHandler x-mapp-php5 .html .htm

If you want to use PHP 5.4 instead:

AddHandler x-mapp-php6 .html .htm

 

PHP Inside HTML GoDaddy

GoDaddy requires two different lines in the .htaccess file, try this:

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

 

Nothing Working?

As a last resort you could also try FilesMatch with the SetHandler method:

<FilesMatch "\.(htm|html)$">
     SetHandler application/x-httpd-php
</FilesMatch>

or:

<FilesMatch "\.(htm|html)$">
     SetHandler application/x-httpd-php5
</FilesMatch>

 

Please leave me some comments if another solution worked for you or if this helped :)

Facebooktwitterredditpinterestlinkedinmail
Author: Dean WilliamsI'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

Post Tags:
, , ,
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments