Written by Handy PHP Administrator
Posted on 25 September 2006
This is a question that I have seen many times in forums and I've decided to sit down and actually explain the process but first we'll examine why someone might want to do this.
Most of the time people are wanting to switch to a PHP website is because they want to add features to their site which requires the use of PHP or they have recently started learning PHP and want to redo their website with their new knowledge. I think the biggest reason is because they want to include external files in their web-pages like a menu file or some other such content that is the same on every page but needs editing from time to time. They just want to edit the one file which makes sense and is what I do when I design a website from scratch using PHP.
What many people don't realize is that there isn't really much converting to do. All you are doing in the end is changing the way that the HTML is generated. When you "convert" from HTML to PHP, you are basically just changing from human generated HTML to computer generated HTML. The browser only reads text and HTML. All your PHP scripts will do is send dynamically generated HTML to the browser.Go to any website that uses PHP and look at the source code and you won't see a single line of PHP code.
So what needs to be converted? Well, in order for a file to be parsed by the PHP parser, it needs to only have one thing done to it! Just change the file extension to .php instead of .htm or .html. That's it, you're done. Do that with every file in your website and you will officially have a PHP based website. Okay, I have converted my website to PHP so why does this tutorial continue?
Simply changing the file extensions to .php on all of your web-pages has left you with a PHP website but now none of your internal links work and any visitors that come to see you just get page not found errors. That's a real shame that your entire website is useless now. So how will we fix that?Well, lets read the entire tutorial first then convert our site!
The first thing you should do is open up your HTML file in some type of text editor like notepad or CuteHTML.
Change any internal links either relative or full URL that point to an HTML page to the same page with the .php extension.
If you are planning on adding some PHP code to your files, then do so at this time. Usually this is where you replace whatever static content with dynamically generated content or replace frequently edited content and/or site-wide content with the include or require function for an external content file.
Finally, save your file using the "Save As" command and be sure to use the .php extension. In Notepad, you will have to change the "Save as type:" to "All Files".
Now you have your first PHP file finished and since you saved it as a new file, your old HTML files will still work until you get ready to delete them. Just repeat that same process for every HTML page in your website.
It is possible to use a script or write a script in PHP that will open a file make whatever changes to the links that are needed, possibly replace static content with PHP or included content, and save the file as a new PHP file. This would work on a search and replace system where you would have to specify what pattern is replace with what content.
The only way to be sure that your visitors get to your new pages is to redirect their requests to the new files. This is done using your .htaccess file.
This is a relatively simple way to ensure that your visitors get to the right page and that you limit the number of dead links due to your conversion. Here is what you do. If you do not already have one, create a file and name it ".htaccess". No more and no less. Add the following code to that file and save it:
RedirectMatch permanent ^(.*)\.htm(l?)$ $1.php
It is a good idea to leave a blank line at the end of the file in case your hosting service offers the option to edit your .htaccess automatically.
The line of code that you just added will take any request for filename.htm or filename.html and redirect it to filename.php. This will take any request for an HTML file and try to forward it to the matching PHP file. You could of course run into trouble if you decide to continue using some of your HTML files since the redirect rule will bypass them now. You'll have to add additional rules for that.
Well, not much to say here. I usually sort my files by type or in this case by extension then select a range and hit delete! Remember, you are deleting the HTML files only. Do not delete your new PHP files nor any of your GIF, JPG, JPEG, MPG, MPEG, BMP, JS, PNG, MP3, WAV, SWF, or any file you need to leave on your website.
Repeat this same process for any directories which contain HTML files you intend to convert to PHP. It is recommended that you check your server logs after a few days to see if you have dead links left. Adding a few more lines of code to your .htaccess file will clean up those missing pages.Enjoy your new PHP based website.