Dynamic HTTP Redirects


Below are some links to scripts that generate dynamic 302 redirects to the current strip of some comics that I read. Yes, this is in direct response to my HTTP redirects are your friend post last year. Read that post for more details as to why I want these (types of) links.

And for a friend who shares my annoyance...

Note: These links blindly redirect to a URL for today's date. The scripts that generate the redirects do not do any logic checking, so they will not work as expected on days that the comic does not produce a script. This is by design, for the simplicity of the script that generates the redirect.

Here's how I have Apache configured:

Add the following line's to your Apache httpd.conf file, likely inside of a <VirtualHost...> section.

ScriptAlias       /path/to/specific/phony/URI       /path/to/cgi-bin/script

Create your script.

#!/bin/bash
echo "Content-Type: text/html"
/bin/date "+Date: %a, %-d %b %Y %T %Z"
/bin/date "+Location: http://www.onthefastrack.com/?webcomic1=%B-%-d-%Y"
echo "Content-Type: text/html; charset=iso-8859-1"
echo ""

Now you can enjoy your own dynamic http redirects too.

A note about the security of the scripts. The scripts do not accept any user input, and are driven solely by the date of the server they are running on. As such, there should be no possible way for them to be exploited.

Update: 2014-01-23

I updated the server config for the redirect links above so that they expire at the time of access. Prior to doing that, they were incorrectly being cached for a week, thus rendering them useless less than useful. Upon initial load, refresh, or cache expiration they did work. They just wouldn't work automatically like I wanted because they were being cached.

Update: 2014-01-26

I added the xkcd redirect (above).

Dynamically creating the URL is a bit more difficult than I felt like doing in SSI. So, here's the script that I'm using to show what needs to be done.

#!/bin/bash
echo "Content-Type: text/html"
/bin/date "+Date: %a, %-d %b %Y %T %Z"
/usr/bin/printf "Location: http://www.xkcd.com/%s/\n" $(date "+((%s - (%s %% 86400)) / 86400) - 14775" | bc)
echo "Content-Type: text/html; charset=iso-8859-1"
echo ""

As you can see, the math to calculate the date is relatively simple in concept, but not nearly as easy to do (directly) in SSI. So, you don't get the URL printed out beside the link.

(SecondsSinceUnixEpoch - (remainder of SecondsInADay from SecondsSinceUnixEpoch)) / SecondsInADay) - XkcdDayOffset) = XkcdCurrentDay