Skip to main content

How to execute PHP code on *.html files

2 min read

Back to blogging after a long while. Hope all you are well.

Today I am gonna share a small tricks with you.
You know it's a default feature of PHP that you can write HTML codes directly on a PHP script. Just write your HTML tags outside the PHP bracket (<?php ?>) or use the echo tag of PHP.
But have you ever thought about to bounce it? That means write your php codes on a HTML file?

I thought. Because in my view URLs end with .html is more SEO effective than thats end with .php. So that I thought to turn around it. Another cause may be or it that's I don't know PHP. :P
Okay, Now it's time to go with the process.
It's not difficult. Just add an extra rule on the .htaccess file for apache, lighthttpd or LiteSpeed and on the configuration file for nginx. The rewire rule is here!


  • Rewrite rule for Apache / lighthttpd / LiteSpeed. (or, Any other which support .htaccess)

    • For *.html files
      AddType application/x-httpd-php .html


    • For *.htm files
      AddType application/x-httpd-php .htm




  • Rewrite rule for nginx

    • For *.html files

      location ~ .html$ {
      root html;
      fastcgipass 127.0.0.1:9000;
      fastcgi
      index index.htm;
      include fastcgi.conf;
      }


    • For *.htm files

      location ~ .htm$ {
      root html;
      fastcgipass 127.0.0.1:9000;
      fastcgi
      index index.htm;
      include fastcgi.conf;
      }




Okay, now you are ready to write and execute php codes directly from your HTML pages.

Caution: This modification will execute everything between <?php ?> and <? ?> brackets. It may conflicts with XML bracket. But don't worry. Use the echo function to echo XML codes.

Bye for now. Mean time let's have some comments on this topic from you!