Generate Static HTML Pages From A Database
85Static 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...
Share it! — Rate it: up down [flag this hub]


johnr54 says:
3 months ago
For those looking to do this offline, Webmerge is a pretty good solution that runs on multiple platforms.