How To Parse HTML Files As PHP

There are many reasons to parse HTML files as PHP. Some webmasters do it because they are converting an old static website into a dynamic website and don’t want to lose pagerank. Other websites do it because search engines seem to favor web pages that have .html endings over those with dynamic .php endings. Or perhaps, you are doing it for security reasons… You don’t want visitors to know what scripting language you use to run your website.

Fortunately, parsing HTML files as PHP easily accomplished on Linux Apache via the use of an htaccess file.

Making the HTACCESS File

If you’ve never seen or heard of an HTACCESS file before, don’t be alarmed. Htaccess files are simple text files that are saved with a .htaccess extension. And they are easily created using a simple text editor such as Notepad or WordPad.

The Code

Believe it or not, you only need a single line of code to do the deed. Unfortunately, the code varies depending on your the configuration of your server. And unless your web host provides documentation, there is no way to know which code will work. But here are typical code samples below. (Note: If these code samples do not work, it may be necessary to consult your host.)

⇒ On web hosts that run two versions of PHP:

Some web hosts which run, or have run, two versions of PHP such as PHP4 and PHP5, usually have a PHP5 handler. The sample code below will parse all .html and .htm files as PHP. (This code has been tested on HostGator and InMotion hosting.)

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

On most other web hosts:

For hosts that only run a single version of PHP, the following code should work. (This code has been tested on Superb and LunaPage Web hosting.)

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

or, if the AddType directive does not work, you can use the AddHandler directive as follows:

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

or

AddHandler x-httpd-php .html .htm

 

Some web hosts, such as GoDaddy require both directives. So your code would look like this:

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

 

As a last resort, you can also try this multi-line approach which uses the SetHandler directive:

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

or

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

Complications

Sometimes, none of the above code samples will work. In this case, there are a few things that you can do:

  1. Consult your web host to learn about the configuration of your server, and determine if some other code should be used.
  2. Verify correct extension. If you are new to htaccess files, it is easy to make a mistake when creating them. The most common mistake is to save the file using the wrong extension. To fix this, you can easily change the filename after you upload to your server. Simply rename the file such that it has no name, but just an extension. So the file name should appear as ".htaccess".
  3. Wait. Sometimes hosts have something called a server-side cache. This means that files take a while to ‘go into effect’. If you are on GoDaddy, for example, you should expect to wait about 30 minutes.
  4. Verify permissions. Make sure that your htaccess files has the right permissions. To view or change permissions, you will need to use the CHMOD utility.

Security & Performance Issues

There are some security issues that you should be aware of, especially if you are on a shared web hosting plan. First, if there is a problem with your HTACCESS file, your file may be able to be downloaded by your visitors or viewable as text. So if there is any sensitive data such as passwords or database information, it should never be stored in an HTML file. Second, if your web host changes the configuration of your server, it may affect the way in which your files are parsed. Third, if you ever plan on moving your website, your HTACCESS file may not be compatible with the new configuration. And finally, it is estimated that websites using this approach are marginally slower than websites that simply opt to use PHP files.

As a result, we recommend this as a great solution for small websites. (Small websites are those with less than 50 pages.) However, as your website increases in size, it would be a good idea to select a more robust solution that will be able to serve a website of any size.

Tags: , , , ,

24 Responses to “How To Parse HTML Files As PHP”

  • Odiin November 15, 2008 at 12:32 pm

    Excellent post. I just spent the past 2 hours trying to find a solution to parsing php on my GoDaddy site and yours worked without any problems

  • David January 8, 2009 at 10:50 am

    Great post… very useful. It helped me out a lot. Thanks Velvetblues… I’ll be coming back in the future.

  • Victor May 25, 2009 at 7:30 am

    Nice post…i use godaddy for 1 website…couldn’t figure out, at first, why the syntax i was used to using was working.

  • Brianoz October 7, 2009 at 10:37 pm

    While this is technically possible, it’s actually not a terribly good idea …

    read this post for more info from several people:

    http://forums.cpanel.net/f6/html-parsing-suphp-132749.html#post576873

    A better solution would be to use mod_rewrite to rewrite the .html into .php, and rename the files as .php. This would avoid the sysadmin concerns without needing much change to the site (you’d need to rename files as .php, but no actual file changes would be needed).

  • JenniC November 4, 2009 at 12:10 pm

    I use biterscripting for parsing html. It works great for me. But, you can try it. There are some good sample parsing scripts posted at http://www.biterscripting.com/samples_internet.html .

  • Greg London April 18, 2010 at 3:16 pm

    Thank you so much for the Godaddy .htaccess code. I called them up and they couldn’t help, but this post did!

  • Stuart Spindlow October 5, 2010 at 11:52 am

    I cannot thank you enough for the GoDaddy .htaccess code!! It has been driving me crazy!!! :)

  • Stuart Spindlow October 5, 2010 at 11:53 am

    GoDaddy should really think about posting this to their Help Forums!

  • Jan January 31, 2011 at 8:28 pm

    GoDaddy code shown did not work today. Ruby, Perl & Python now come standard on server – don’t know if that’s the hangup.

  • Jan January 31, 2011 at 9:18 pm

    I got this error with the GoDaddy code

    Forbidden
    You don’t have permission to access /index.html on this server.

    Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

    ——————————————————————————–

    Apache Server at mainharborstore.com Port 80

  • Bill January 31, 2011 at 9:38 pm

    Thanks for the info, it was quite helpful, but I still had to call my host, GoDaddy, to get this working. In the end it works, and my .htaccess file looks like this:

    Options +ExecCGI
    AddType application/x-httpd-php .php .htm .html
    AddHandler x-httpd-php5 .php .htm .html

    I hope this helps someone else having the same issues.

    • Henrik B February 8, 2013 at 8:28 am

      Bill,

      Thank you for that code. Just used it to setup a new GoDaddy account and it is the only one that works on several that I have tried!

      Best
      Henrik

  • Vernon Morris May 21, 2011 at 7:12 am

    Bill, that was a life saver. I just upgraded my Godaddy hosting to the 4Gh…and your code was the only one that worked

  • badar July 9, 2011 at 10:50 am

    Nice post…i use godaddy for 1 website…couldn’t figure out, at first, why the syntax i was used to using was working.

  • Prince Vasquez July 10, 2011 at 9:45 pm

    This saved me a whole lot of researching. Thanks for this. I hope your codes above will work on Crazy Domains (Australian Hosting). I tried it on Hostgator and works perfectly fine.

  • Stuart September 4, 2011 at 12:09 pm

    Once again have come back to this post which has saved the day! GoDaddy have recently upgraded their Hosting Configuration to Apache Version 2.2 and so the original .htaccess code no longer works. However thanks to Bill for the new .htacccess code php parsing is working again! You can check the hosting configuration apache version on the hosting dashboard.
    Thanks again!

  • Iswandi December 22, 2011 at 2:20 pm

    Hi,

    Thanks, it works just fine for my case. Before I had “AddHandler application/x-httpd-php .html .htm” which didn’t work than I changed into “AddHandler application/x-httpd-php5 .html .htm” like the first example above, then works fine

    Cheers

  • Mike January 9, 2012 at 9:28 pm

    Bill, nice work on the Godaddy change. Could not figure it out but Bills post of:

    Options +ExecCGI
    AddType application/x-httpd-php .php .htm .html
    AddHandler x-httpd-php5 .php .htm .html

    Works Perfectly.

    Thanks Bill you ended my search

  • steve February 23, 2012 at 2:00 pm

    just switched hosts to godaddy.

    this works:

    Options +ExecCGI
    AddType application/x-httpd-php .php .htm .html
    AddHandler x-httpd-php5 .php .htm .html

    thanks to bill’s note January 31, 2011 at 9:38!

  • Don April 29, 2012 at 12:26 pm

    Hello,

    This post was such a great help. I tried to find this information for a while and nothing worked until I found this post…thanks!

    I am using Dreamhost for some websites and used the following code which worked:

    “AddHandler application/x-httpd-php5 .html .htm”

    I am adding a wordpress blog to a subfolder and did not want the htaccess code to affect the blog files.

    www.maindomain.com/blog/

    How do I exclude the blog files (exclude subfolder) from being affected? I read that the rewrite rule via htaccess works best on smaller websites with a low number of webpages so I do not want the .htaccess to even look at the blog files.

    Thanks for any help!

    Don

  • David Terrazas June 1, 2012 at 6:28 am

    I can get it this version

    ****************************************************
    Options +ExecCGI
    AddType application/x-httpd-php .php .htm .html
    AddHandler x-httpd-php5 .php .htm .html

    *****************************************************

    to work on a shared server on GoDaddy, but not on our VM instance….any advice or suggestions?

  • Karan October 22, 2012 at 2:52 am

    Hi,
    I really need your help regarding an issue I’m facing with .htaccess file.
    I’ve recently moved my website from html to php. Now, I want to redirect few old html pages to new php pages. The problem is that the file names are different.
    Example: How to redirect: http://www.sagaciousresearch.com/Patent_Filing_Support.html#

    To
    http://www.sagaciousresearch.com/services_and_pricing.php

Trackbacks

nGeeks.com

Trackback URL:

Leave a Reply

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com

Go Daddy Deal of the Week: Cheap .COM Domains! Offer expires soon!