Htaccess rule creator for new to old urls

Here is a tool to create redirection rules, primarily for rewriting pages based on a query string to a nice seo url.
Created because .htaccess files are the easiest way to break an entire site.

Old Url:
New Url:

The generated rule would be:

RewriteCond %{QUERY_STRING} ^page=123&something=else$
RewriteRule .*  htaccess/rules/? [R=301,L]

It can’t handle crossing domains (yet).

Easy php output compression

Below is what I regard as a reliable method of doing gzip output compression.

One thing that normally breaks gzip compression is something printing/echoing before the ob_start (usually the result of an error, debug output, errant space somewhere etc…), this is where the ob_get_length and ob_flush come in.

The problem is that headers_sent is not a reliable way to tell if any thing has been output because it may still be in the output buffer, so if we flush it before hand problem solved.


function doPage()
   echo 'Hello World';

if (ob_get_length() > 0)

if (!headers_sent() &amp;&amp; @ob_start("ob_gzhandler"))
   //Probably a good idea to exit here as more output would break it

lighttpd, php and clean urls

Here’s a situation, You have php running on a lighttpd server via fastcgi and you want clean urls (ie. /post/123/postname).

The typical way to-do this with lighttpd is to set the error-handler to your script.

$HTTP["host"] == "" {
    server.document-root = "/var/www/"
    server.error-handler-404 = "/index.php"

We use an error-handler here rather than rewriting the url as with apache because there’s no way to detect if the url is actually a file (ie. your css/js files).

Problem here is that this will break query strings for anything other than the root url (query strings are what’s after the ?). ie. when accessing /post/123/postname/?confirm=true the confirm will not appear in the $_GET or $_REQUEST arrays while /?confirm=true will.

So how do we fix this? The method I’ve used is to manually parse the query string and put its values into the get and request arrays, as below.

function fix_query_string()
    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $query = parse_url($url,PHP_URL_QUERY);
    if ($query == False || empty($query))
    $_SERVER['QUERY_STRING'] = $query;
    foreach($query as $key=>$value)
        $_GET[$key] = $_REQUEST[$key] = $value;
if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false)

Hope this helps!

Threaded flicker-free control rendering in c#

I recently ran into a problem where a control was rendering slow enough that it caused flickering (as the screen region is cleared and then redrawn).

Looking around the ‘net I settled on rendering the control to a bitmap and then drawing the bitmap to the screen (fast operation), which is called double buffering.
Continue reading

Text block clock


This is a clock that tells you the time by highlighting text in a block of text. In this case using a C# Console App. Not exactly original, but I thought I’d give it a try.
The progress bar counts the time until the next 5 minute mark.

Download Code
Download Executable
Continue reading

Maximizing a print preview dialog in c#

Here is a way to get a print preview dialog (or perhaps any resizeable dialog) to be maximized when opened.

The dialog may be cast to a Form allowing WindowState to be set.
For example:

PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = doc;
((Form)dlg).WindowState = FormWindowState.Maximized;

Detecting drive insertion and removal in C#

Here is some C# code to detect when a logical volume (e.g. USB Memory Stick) is inserted or removed via the WM_DEVICECHANGE message with WndProc.

However this doesn’t tell you what has been inserted/removed, you will have to poll the drives manually to find out.
If you need that you will have to register to receive events from windows which involves P/Invoking RegisterDeviceNotification (See Drive Detector at Code Project).

using System.Runtime.InteropServices;
using System.Security.Permissions;

public class MyForm : Form
        const int WM_DEVICECHANGE = 0x0219;
        const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
        const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //device was removed
        const int DBT_DEVNODES_CHANGED = 0x0007; //device changed
        const int DBT_DEVTYP_VOLUME = 0x00000002; // logical volume

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
            if (m.Msg == WM_DEVICECHANGE
                &amp;&amp; m.WParam.ToInt32() == DBT_DEVICEARRIVAL
                || m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE
                || m.WParam.ToInt32() == DBT_DEVNODES_CHANGED))
                if (m.WParam.ToInt32() != DBT_DEVNODES_CHANGED)
                    int devType = Marshal.ReadInt32(m.LParam, 4);
                    if (devType == DBT_DEVTYP_VOLUME)
                        //Poll drives
                    //Poll drives

            base.WndProc(ref m);

Live Mirror

Here’s a C# program to mirror all changes in one directory with another.

I build it to let me edit a PHP web project on my development PC and have it mirrored to my development server, without any action on my part.

Its all being hosted at Google Code, go on give it a try :)
Continue reading

MySQL backed SMTP+IMAP Mail server on Debain Lenny

This is a guide for setting up a mail server with a mysql backend for users and domains using Dovecot for the IMAP Server, Postfix for the MTA and SpamAssassin for preventing spam and all with SSL client-server encryption.

I’ll also assume you have a MySQL server ready for use.
Continue reading

Smullyan’s Paradox

Here is one of the best paradoxes I’ve seen in a long time:

“At a desert oasis, A and B decide independently to murder C. A poisons C’s canteen, and later B punches a hole in it. C dies of thirst. Who killed him?

A argues that C never drank the poison. B claims that he only deprived C of poisoned water. They’re both right, but still C is dead. Who’s guilty?”

[Via Futility Closet]