Tuesday, June 19, 2007

Digg effect took down your website. How to come back online with only a few lines of code.

So you have a really powerful and expensive server but you haven't done any optimizations for your website which is probably a reason why it's down now, after only 30 diggs. Don't worry, there's a quick fix to it and only takes a couple of minutes to do.

Your website is dynamic (probably PHP & MySQL). Each time somebody visits a page, your server has to run a php script which would connect to MySQL database and run some queries. Average PHP script would eat so many resourses that without any optimization, Digg Effect would be fatal. But what if instead of that dynamic page you'd have a static .html file there? No need to hit php, no need to hit MySQL - it would run even faster than a script with a really good optimization and much more faster than your current script (maybe up to 50 times, or up to 100 times - depends on how bad is your script) which simply means more visitors can see your page.

Let's say your URL is: http://MySuperWebsite.com/index.php?page=AwesomeArticle

So here's how we'll do this:
  1. Save your dynamic page (which is under heavy load at the moment) as a static .html file
  2. Edit .htaccess file. You'll need to turn your dynamic page into static:
    instead of opening http://MySuperWebsite.com/index.php?page=AwesomeArticle
    it should open http://MySuperWebsite.com/AwesomeArticle_static.html
    So when somebody visits this page, static file will be served instead of dynamic even though you see .php in the URL - no php and no MySQL will be touched.
    We'll use mod_rewrite for that. Here's what you need to put to your .htaccess file:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^page=AwesomeArticle(.*)$
    RewriteRule ^index.php$
  3. Save it and watch your website getting back online. All your website is dynamic except this page. This is just a temporary fix. You can now focus on optimizing your scripts.


Put a Poll in your Sig - SigPoll.