logo
Page Affairs

Editing and Web Design

Running PHP on HTML Pages

Normally, if you want to run PHP scripts on a web page you will give that page a .php file extension. Otherwise, the PHP script will not be parsed (or executed). However, if the page already exists with a .htm or .html file extension, it is not convenient to change the extension. (Unless the page is an index page, you will effectively be creating a new file, and will lose any page rank that the original page had, and any links to the old page that are not updated will be broken.)

Fortunately, there is a simple alternative to changing the file extension. You can instead add a simple instruction to a .htaccess file. (See below if you aren’t sure what this is.)

[Before I proceed any further, I am pronouncing .htaccess as “dot htaccess”—hence the use of “a” instead of “an”. Apologies if this is not the way you pronouce it.]

To enable PHP to run in your .htm or .html pages, simply add this code to your .htaccess file:

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

If you want to apply this rule just to a particular .html (e.g. mypage.html), rather than all of your pages, you can target it like this in your .htaccess file:

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

Accessing the .htaccess file

A .htaccess file is a simple text file that normally resides in the root folder of your website—that is, at the same level as your site’s home page. Usually, if you visit the online control panel of your website, you will see a default .htaccess file. (You will probably need to select an option to View Hidden Files, as files like this—i.e. ones that start with a dot—are often hidden by default.) Once visible, you can right-click this file and choose Edit to open it and make changes.

If such a file doesn’t exist, your host may or may not let you create one. After choosing to view hidden files, try to create a new file called .htaccess. If this is successful, proceed with the steps above.

You can also access .htaccess files from your web editing software. Again, being a hidden file, a .htaccess file may not show up even if it exists, so first make sure that you have set your preferences to show hidden files. (For example, in Dreamweaver, click the options dropdown on the Files tab > View > Show Hidden Files.)

If you can’t see a pre-existing .htaccess file in your editing program, you can create one. It’s likely that your software won’t let you edit a file beginning with a dot, so go into remote view and create a file with a name such as htaccess.txt. Open this file and paste the code shown above into it. Save the file and close it. Then change the name of the file to .htaccess and upload it. With any luck, you can now add PHP to your .htm and .html pages.

Acknowledgements & Links

© Page Affairs, 2008–2024