Tech-Evangelist

Technical Articles, Musings and Opinions from Tech-Evangelist

  • Home
  • About
  • Guidelines
Previous article: Moving a WordPress Blog To a New Hosting Company – WXR Method
Next article: Affiliate Marketing Programs Under Attack Due To New State Tax Laws

Converting HTML Pages to Run PHP

April 21, 2011 By Doogie - Copyright - All Rights Reserved

This issue frequently arises when site owners want to convert legacy .htm or .html pages to run PHP, but they do not want to change the file name extensions due to the potential loss of rank positions when the URLs are changed. It is easy to convert HTML pages to run PHP if you know how and you can change those static pages to run dynamic PHP code.

First, the site needs to be on a Linux or Unix server running the Apache web server. This will not work on a Microsoft server. You also need to use a hosting company that allows server overrides using the .htaccess file. Almost all except the extreme low-end hosting companies allow server overrides, but that question needs to be asked before you commit to hosting services.

The .htaccess file can be found in the root directory. It is a hidden file, so you may have to configure the FTP utility to display the file. if the file exists, download it to your PC so that you can work on it. If it does not exist, create the file. You must use a pure text editor when working on this file. You cannot use a word processor. Any errors or embedded code will cause your site to crash with an error 500.

The first method simply tells the server to run all htm and html files through the PHP processor. Just add the following directive to the .htaccess file. There are two variations, depending upon how the server has been configured. Some trial and error testing is required to see which method works with your server configuration.

This code should work on most Apache servers that are configured to run PHP as a module.

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

Some servers run PHP as a CGI. If the first method does not work, try this method.

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

To test this, simply add some PHP code to one of your HTML scripts, such as the following. Only the words, “this is a test” should display.

<?php echo "this is a test"; ?>

There is another method that can be used, but it requires some thought as the site is converted to PHP. This method uses the mod_rewrite function that is built into Apache to rewrite all of the HTML URLs to the equivalent PHP URLs. This means that all of the internal links in the site will continue to use the htm or html extension with the URLs, but the actual file names will use the php extension.

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]

This example works with .html extensions. If your site uses .htm extensions, then change the html to htm in the RewriteRule line.

Once again, all of the actual page scripts need to use the php extension, but all of the links within the site will continue to use the legacy .htm or .html extension. The rewrite rule converts the .htm or html extension. The rewrite is performed at the server level. You should not see the URLs change in a browser’s address bar. As far as your site visitors and search engine spiders are concerned, the pages still use the exact same URLs as they did before the conversion, but you now have the ability to create dynamic pages that run PHP code and can pull data from a database.

Filed Under: PHP Tutorials, Web Site Development

Categories

  • Affiliate Marketing
  • CSS Tutorials
  • FileZilla Tutorials
  • Home Theater
  • Internet Marketing
  • Internet Technology
  • Kindle Tips
  • MySQL Tutorials
  • Online Auction Tips
  • Paint Shop Pro Tutorials
  • PHP Tutorials
  • Tech News
  • Thunderbird Tutorials
  • Video Production
  • Web Site Development
  • WordPress Tutorials
Content and images are copyrighted by Tech-Evangelist.com and others

Copyright © 2023 Tech-Evangelist.com - All Rights Reserved
Posted code samples are free to use. Do not reproduce or republish articles or content on another web site.

Privacy Policy : Terms of Use