How to run PHP with HTML extension?

There are a couple of reasons why you may want to run php with html extension. You may have a static website with highly ranked web pages, and would like to retain search engine indexing and ranking while converting the site to use server-side technology. Or, you may not want to reveal server-side technology to your website visitors for security reasons. While others may just prefer plain old html extensions over php extensions on their URLs. Regardless of your reasoning, using a server-side technology such as PHP with html extension is easy to setup.

On the LAMP platform where PHP technology is often used, using php with html extension can be setup with .htaccess file. The .htaccess file is a configuration file used with Apache web server to customize the functionality and features of the web server. You may create a .htaccess file in any directory within the document root, and when present the containing folder and sub-folders will inherit the configuration defined in the .htaccess file. You may create another .htaccess file in a sub-folder, and it will override the configuration defined by it's parent configuration.

To configure Apache web server to treat files with html extensions as if they are php files, you'll create (or edit) the .htaccess file. This setup can also be added to the Apache configuration file, httpd.conf.

AddType application/x-httpd-php .html

You may further customize the .htaccess file to only make the configuration effective to one php file. To setup only a limited number of html files, you may use the following format:

<files filename.html>
AddType application/x-httpd-php .html

You may also use <files>, <filesMatch>, and <location> directives.

** One downside of setting up a web server to use html extensions for php scripts is performance. Every html file will run through the php parser, and it will have a minor performance impact which will not likely be noticeable.

To upgrade PHP 5.1.x to PHP 5.3.x on Linux, please read PHP Upgrade article.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.