March 26, 2010 Archives


Fri Mar 26 12:49:32 EDT 2010

A BASH CGI as a workaround to .htaccess files on lighttpd

As you might remember, in a previous post I explained how to maintain a permanent link which always points to the latest submission on your nanoblogger website. Since I decided to migrate from apache to lighttp I lost the ability to use .htacees files thus .htacees file based redirects do not work anymore.


The workaround.
We will keep using the /latest url for the redirects the only thing is that we will use a CGI written in BASH to accomplish the redirect. The script will parse our RSS feed (rss.xml) to check which is the latest post and redirect to that url. The redirect is accomplished by sending the HTTP headers 'Location' and 'Status'. Let's take a look at the script:
latest.cgi

Here we are sending the Status header to prevent search engines from associating more than one url to the same content (this will give us a lower score in some search engines' systems) and then we send the Location header to do the actual redirect.


Lighttp and CGIs.
In other to execute this CGI we need to make some minor modifications to our VirtualHostost configuration. Lets add these two lines to it:

In the first line we are telling lighttpd to use /bin/bash to execute any *.cgi file (you can change this to anything like *.sh, *.bash). In the second line we redirect all requests to /latest/ to the url /latest.cgi. You can either use /latest.cgi or /latest/ for these urls in your signature at the forums; personally I like to use /latest/ to keep track of where is my traffic coming from even if I don't get the HTTP referrer.


Spanish version: Un CGI en BASH para mantener tu firma al dia en todos los foros en que participas.


Posted by orvtech | Permanent link | Comments | File under: Scripts, NSLU2, Linux, nanoblogger, lighttp