PHP into .html file

If you need to add php code into .html (or .htm) file and you don’t want to change the file extension to .php because you already have incoming links on the .html file - although you can solve that with a simple redirect - here is the solution:

Add the following lines of code to your .htaccess file:

AddType application/x-httpd-php .html

If you just need one .html file to behave like that, then enclose that code with “files” tag like this:

<Files somepage.html> AddType application/x-httpd-php .html </Files>

Pretty simple, right? :cool:

Here is what’s happening - when a web page is requested, the server checks the file extension to know how to handle the page. If it sees a .php extension (or .shtml, .asp etc.), it knows that it needs to execute the appropriate code before passing it along to the browser. If it sees a .htm or .html file, it sends it right to the browser because it doesn’t have anything to process on the server.
With the above piece of code, we simply tell to server to execute a .html file as if it’s a .php one.

    Be aware!

  • If you have an existing .htaccess file, make sure not to overwrite anything there or other settings may stop working!
  • Anything in your .html files that starts with <? will now be executed as PHP, so if it’s in your file for some other reason (an XML tag for example) you will need to echo these lines to prevent errors. For example:
    <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>

After you made the changes to .htaccess file, create a test file and copy the following html into it:

<?php echo "PHP code detected !!!"; ?>

Upload it to your web server and view it using your browser. You will see that it works just fine.

Tags: ,

Popularity: 32% [?]

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.5 out of 5)
Loading ... Loading ...

Check out this video!!

Write a Comment

Take a moment to comment and tell me what you think. Some basic HTML is allowed for formatting.

You must be logged in to post a comment. Click here to login.

Reader Comments

For somebody who is only getting started like me that is an excellent tip and thank you very much for posting it.

thanx for the lesson,google love html pages more than php
keep up the good work:)

the code didn’t work …what is the problem i couldn’t analyze…when i request a page…instead of opening the page…it starts to download…plz help me solve this problem

Try this then:
AddType x-mapp-php4 .html .htm

I found this article to be extremely useful for me. Thanks :razz:

For some reason I am having a hard time with this…same as another commenter here! I thought for sure I had it figured out after reading this. I’ll keep trying.

Thanks for your lesson, you help me

Thanks for the handy tip, html is my preference, I was bought up using html when I first started learning about websites. Thanks once again,

Excellent tip for those who started like me. Google give preference to HTML pages more then PHP pages. Good work. Keep it up.

Hi, I am newbie to this but the tip you have given is damn easy thanks!

You can also discard all file extensions altogether because in some years your site may be using another technology and you won’t want to change your URLs (they may be been referenced all over the world). You can configure your web server to do that for you regardless the file extensions it uses internally.

RewriteEngine on
RewriteBase /
RewriteRule ^([^/]*)\.html$ $1.php?%{QUERY_STRING} [NC]

I have an informational website, Steinway Pianos New York, that I had to overhaul to manage using a CMS. In my case the opposite was the case, I had incoming links to .php files that I converted to .html so I solved that problem by creating a .htaccess redirect like this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^old-php-file\.php$ “http\:\/\/www\.yourwebsite\.com\/new-html-file.html” [R=301,L]

That’s it!

Follow me on Twitter