PHP can be useful when used in conjunction with html. Even if you don’t plan create full blown dynamic pages certain functions like server side includes can help you repeat the code less for commonly used things like menus and advertisements. But it can be irritating to end the file with a .php extension just because you’ve added a snippet of PHP code. Also some people say that pages with a .html extension rank more in search engines when compared to .php. I don’t know how far its true and I’ll leave it in your hands to experiment.
In this article I’ll focus on the things needed to be done for html to be parsed by the server. (More about this word later)
Step 1: Create/Edit the .htaccess or httpd.conf file
In case you’re running your own web server or having a dedicated server/VPS hosting your website the you’ll definitely have access to the httpd.conf file. Open the file using a text editor, go to the end of the file and add the following line.
AddType application/x-httpd-php .html
Save the file httpd.conf and restart Apache server for the changes to take place.
If you’re hosting your website on a shared server you can do the same thing using the htaccess file. Create a file name .htaccess (not the dot at the beginning) if it doesn’t already exist or edit it if it exists. Add the same line shown above and save the file. If you follow this step you don’t need to restart Apache Server. If you want to add .htm extension too you can do so in the following manner.
AddType application/x-httpd-php .html .htm
Step 2: Testing the New Configuration
Now its time to test if the changes made actually work. Create any file with a .html e.g. phpcode.html. Write the following PHP code to that file.
print “<h1>Your IP address is ” . $_SERVER[‘REMOTE_ADDR’] . “</h1>”;
Save the file and access it via your browser by typing http://yourwebsitename.com/phpcode.html. If you’ve done everything right you should see the sentence “Your IP address 192.168.1.1” or whatever is you IP address in heading 1 form.
If you get a 500 Internal Server Error check the syntax in the htaccess file. Another possible error is you see the PHP code displayed in the browser. In this case if you’ve used the httpd.conf method you should restart Apache Service and make sure created a test file with the same extension as entered at the end of AddType i.e. if you enter the .html extension and create a test file with .htm extension it won’t work. If this is the case you better add both the extensions in AddType.
What is parsing?
Earlier I mention the word parsing. Parsing is the process of analyzing the code in a particular file and executing it the way it should be executed. For example if a web browser sees a <b> tag it renders text in bold format till the it encounters a close </b> tag.
Similarly Apache Web server “parses” the code in each and every file with a .php extension and executes the PHP code. Now by adding the snippet AddType application/x-httpd-php .html we are telling the web server to treat files with a .html extension the same way it would treat a php file. So now the web server will parse .html files also.
This is a HORRIBLE idea. I just worked with someone who quoted this post and has been putting PHP in HTML files for years. PLEASE either remove this post or edit it to say that this is not in any way recommended.
Jesin A says
A Google search for “parse html as php” returns results linking to sites like StackOverflow with similar answers written by people with a very high reputation (like this and this). I’m not saying this is a good idea, but I believe someone using this method over URL rewriting has different priorities when programming.
This post is a great help for someone how wants the php code parsed in the HTML file.