Generate Static HTML Pages From A Database
By peeling
Static HTML vs. Dynamic Pages
I have a rule, when it comes to webpages. Keep it simple. No scripts, no flashy templates. It's no big secret that the most successful websites are those which are the simple and easy to use. See Google. But that doesn't mean that you can't leverage technology behind the scenes to provide users with a better experience. The specific problem I'm addressing here is whether to use simple and static HTML pages, or generate pages dynamically from a database, using PHP/mysql.
Static HTML pages have obvious advantages - Fast loading, easy to write, less load on the server, lower bandwidth costs, search engines prefer static pages over dynamic ones ( generally speaking ).
Dynamic pages have their own set of advantages - Automatic refreshing of content ( news, sidebar updates, etc. ), capability to interact with the user ( web query forms, data output, ecommerce applications, etc. ).
But I want to have the best of both worlds. I want to have a static HTML page, which is served out of a database, and will refresh itself automatically at pre-specified intervals. How is this done?
You don't need to be a programmer to implement this on your tiny personal website. Anyone can do this. You just need to have a webhost who allows you to run PHP scripts, a mysql database and do some trial and error.
I'm assuming that you have need of a database driven application on your website. It's overloading your server or loading real slow, and the search engines don't like it. Which is why you want to switch to static, but retain the database output. If you don't have this need, well, head over to entertainment to check out how Paris Hilton is doing ( She's doing fine - In an LA Prison ). Update : Psychiatrist rushes to see Paris. Paris Hilton is dynamic and the search engines love her. PHP code vs. Paris Hilton - No contest this...
Take one of your PHP page as a test subject. All you need to do is add a bit of code to the top and bottom of the page, as shown below.
PHP Code
Explanation
What we're doing here is basically generating a cache of the parsed output of a PHP page. Change 'your-folder/your-file.html' to point to the location and name of your static html file. The folder and file should have write permissions ( 666 ).
ob_start() starts the caching process.
Just below this is where the entire contents of your original PHP page should be.
End of that, add the code snippet as shown in the image above.
And that's that. You should now have a static HTML file which is the same as the parsed output of the original PHP page. Notes : If you want to save even more bandwidth, just set up a cron job to call the PHP page once in 24 hours, and you can skip the code for the cachetime. So, in that case your code would be something like this :
PHP Code 2
Summary
Related Links
- Static vs. Dynamic urls - Google Video
Matt Cutts answers Google questions: - Static vs. Dynamic URLs, and how search engines crawl pages. - Static Versus Dynamic URLs
Database-driven sites are great but it will be much better if you serve your pages to the search engines and users in a format they can easily handle. - PHP: A simple tutorial - Manual
Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic web page creation with PHP... - PHP: fopen - Manual
The fopen() function opens the file whose pathname is the string pointed to by filename, and associates a stream with it...
Comments
Or a better way is to have an .htacces to run html files as php
Does this come under the category of black hat techniques?
I have a list of towns and cities that I would like to spit out as static html pages but feel it may be spammy?
Andrew
Good site, thanks for the info
Is it possible to make this work with wordpress?
Selva - It works on any PHP page, but since wordpress already offers dynamic .html pages, I don't see why you'd need to do this on wordpress.
Well, the article doesn't explain how to generate static pages starting from a database (i mean a database like MySQL or MSSQL), but instead it explain how to use a file-database system. In more cases a file-database solution can be the right choose against using 'standard' databases, due to its high speed (there is NO database that crunch the server processor). Of course, the updating/generation mechanism of such a website should be very well planned from the very beginning in order to avoid massive manual updates.
@Andrew: this has nothing to do with BH
Noticed your comments and this is something I'm trying to work out myself. I use CofferCup's HTML editor and have been building my own sites for about a year, so I've got a lot to learn.
I like the idea of static pages but it seems so much more sensible to publish these from a database before they are uploaded to my web server. Managing the content in a database appears so much easier but how can you do this easily?
Thanks for your advice. I eventually purchased a web merge for the project I was working on and it built me over 1100 pages in about 5 minutes - really saved my bacon.
Thanks again
Thank you! Exactly what I needed -- perfect for generating static files from WordPress 3.1 wp_nav_menu() so I can have the same menus on external pages! I kiss your feets!!
Hey peeling man you made my day. was searching and got the solution for php to static html pages. Your simple code worked gracefully.
Thanks for sharing the knowledge.
Best Regards
Many Thanks for this post. saves a day!
This is pretty cool, but what would be cooler would be to automate this so you don't have to wrap each existing php file that may generate output. Also -- this should only be done for files that *do* generate output, obviously.
I'm trying to do this for a few pages that originally html pages that have php included in them so had to have the php extension on them. So, the code encountered an error upon trying to read the doc type tag above the head section. Any way to do what I want?
Nice post. Here’s a tool that lets you build your online database in minutes, without coding http://www.caspio.com/
allok
johnr54 4 years ago
For those looking to do this offline, Webmerge is a pretty good solution that runs on multiple platforms.