Changing the header image every month

One of the things I did with the old design of this blog was to change the header image every month, and it's something I use on my other website ( I could have done this by uploading a new image every month, but being a programmer I decided to do it a different way.

Using mod_rewrite to change the image

If you're using an Apache server, you can use something called mod_rewrite to rewrite URLs and do other nifty tricks. I used this to change the image the user saw every month.

I keep all the images in my images folder, and name them according to the month I want them to show. For example, the March image will be called "03-header.jpg", and the December image "12-header.jpg". I then use a little mod_rewrite trickery to redirect requests to the correct location. The code looks something like the following:

<ifmodule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^images/header.jpg$ /images/%{TIME_MON}-header.jpg

This code is placed in a ".htaccess" file that resides in the html root folder on the server. This is the folder where all the other folders and files are kept. I put it here instead of in the blog folder because WordPress has its own ".htaccess" file which sometimes changed and removed my tweaks.

Using it in the template

You don't have to make any changes to your html templates, just make sure you link to the image as "header.jpg" and not something like "12-header.jpg". The server will take care of all the redirection all on its own, and it won't disturb anyone's browsing experience.

Post a comment

org-mode tags allowed: /italic/, *bold* and =code=