html as php .htaccess

So with my shared hosting I had this trouble where I have to include a bit of php code inside the actual html file, but the web server won’t process it be most web servers are configured specially on shared hosting to treat “.html” files as HTML (no dynamic code gets executed except SSIs if they are configured) and files ending with “.php” as PHP. Now what if you have to process html file as php or run a php code inside your html file. Luckily with apache we have .htaccess file available which is there to override the default setting in your httpd.conf file.

Ok now lets see what you can do to execute html as php. In your .htaccess file copy paste the config setting below

The above setting will work if the apache is picking php as a module. what if apache is executing php  as CGI?

Then add the line below to your .htaccess file

You can apply the same technique to treat other file extensions to be executed as php, for example if you want to use .cgi or .aspx or .jsp as your file name extension then you could do something like this to

AddType application/x-httpd-php .cgi .aspx .jsp

AddHandler application/x-httpd-php .cgi .aspx .jsp

To master .htaccess you must read this article as a starting reference point

If you are using php 5 then you could also try the config options below

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

and same applies to php 5 running as cgi, rather than using AddType use AddHandler as shown below

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

End of the day it all depend on where you are hosting your website and what recommendation your host makes about this.

For people who are using cpanel with php 4 and 5 most likely this will work

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

and when php runs as cgi replace AddType with AddHandler


I hope this helps





VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)


  1. Sawan Kumar says:

    .htaccess is very confusing and when java provide url rewriting then why we use .htacess and by this you can easily convert your url seo friendly.

    paste below code in WEB-INF/web.xml



    And you need to import their jar file.
    Step is given on this blog where i found this tutorial

Leave a Reply