Mar 22, 2010

10 Ways To Use PHP On Your Website #4: Using PHP on HTML Pages

eLearning Series

by 

Have you ever wanted to make a site more dynamic, but didn’t want to have to change all of the page names? There’s an easy way to do this…

read more below >> Ten Ways to Use PHP On Your Website Tip #4: Using PHP on HTML Pages

How can I use PHP code if my pages end in .HTML? One of the biggest challenges when upgrading the functionality of a website is changing all of the page names. For example, if you have a site that has pages that end in .html or .htm, this means that you usually don’t have the ability to use some of the time-saving features of php scripting.

 

For example, the navigation can’t be placed in a separate file that you can update everything in one area (vs. having to edit each and every page of the site). Or, you couldn’t pull off last week’s tip… personalizing a site based on geographic location. Most web developers will tell you that in order to use php, you need to change all of yourfile names to .php. So the about.html page would change to about.php. It’s painful to rename all of your pages This is less than ideal for many reasons. Why?

First, you may have lots and lots of links inside your site that would then need to be changed, often one at a time, in order to prevent broken links.

Second, other websites may be linking to one or more pages of your site, and changing the navigation can cause you to lose the value of inbound links, both from a traffic perspective and a search engine popularity perspective.

Third, many search engines may have already indexed your site successfully. If you change all of the page names, a search engine will then have to re-index all of your content, and you risk losing position.

While there are ways to use 301 redirects to make sure that the old page name will automatically forward to the new page name, but for a website with many pages, this can be a long and painful process. The Work-Around One way to work around the limitation of php scripting is to essentially tell your server that an HTML page (i.e. a page ending in .html or .htm) should be treated just like a page that ends in .php. The way to accomplish this is fairly technical, but if you have an Apache-based Web server, you can modify the .htaccess file on the server: Add this line into the .htaccess file: The .htaccess file is generally where the error 404 handling is set up for your site:

Edit your .htaccess file to enable html pages to run php scripts

 

Then, you can add php code into your .html pages, such as: php include file code

And it should work properly! Summary: There is a way to trick your webserver to think an HTML page can run php scripting, allowing you to add interactive features, but not have change all of the names of your pages.