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.
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.
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>
Sometimes, none of the above code samples will work. In this case, there are a few things that you can do:
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: Apache, cache, htaccess file, Linux, PHPTrackback URL for this entry:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/trackback/
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
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
Great post… very useful. It helped me out a lot. Thanks Velvetblues… I’ll be coming back in the future.