One of the most common questions hitting our support inbox is how to get PHP working in *.html and *.htm files. With this blog we try to provide a practical guide for our users and thus kill two birds with one stone figuratively.
Question One: Does my web host support PHP?
At first you should try to find our whether PHP is supported by your webhost or not. If you already know that PHP works please continue to read at Question Two. If you don't know you could either ask your hosting company or you could just try it by following these steps:
First, create a file named "phptest.php". At this stage it is important to use the file extension ".php". Open the file in a simple text editor (for example the "Editor" in Windows) and write the following line:
<?php echo "hello world"; ?>
After saving the file please upload it to the root directory of your webspace. Open your browser and direct it to the file you just uploaded: "http://www.your-domain.com/phptest.php" (Needless to say, you should use your own domain here...)
It's the moment of truth now. What does the browser say? If you see the words "hello world" everything is fine, PHP is working. However, if you don't see these words please contact your hosting provider because PHP is not working (yet). Most hosting companies provide packages including PHP. So perhaps you just have to switch to another hosting plan.
Question Two: How to get PHP working in HTML files
If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."
You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.
First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file.
(Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess")
Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file:
AddType application/x-httpd-php .html .htm
If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5:
AddType application/x-httpd-php5 .html .htm
Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.
You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:
Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...)
If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.