Today I had the pleasure of having one of my websites slammed with traffic. Normally this would be a wonderful thing, filling me with the joy of a thousand nerds meeting Jeri Ryan in person. I quickly determined that I wasn’t getting the kind of traffic I wanted. Some wanker had directly linked to an animated GIF on my website EnjoyTheRandom.com from Reddit rather than linking to the post it was on. This is generally considered awesome by Redditors and heinous by webmasters. We spend a ton of time combing the interwebs for content, so the least you can do is ignore the rest of the site while you get a chuckle out of the free content we’re providing.
Normally I’d just let it slide, but this was a nearly 2MB animated GIF and the site was burning through over 5GB of bandwidth per hour. Since a moderately popular post on Reddit can generate that kind of traffic for a day or two, and because the GIF had been re-posted on a few other blog sites, I decided I couldn’t sit back and eat the bandwidth.
I had a few options. I could simply remove the GIF from the site, but that would look like the site was down or couldn’t handle a decent amount of traffic. Alternatively I could redirect the image to the post itself, which was the optimal solution. That way, the visitor could view the content they were looking for but I also got to present the rest of my site and gather analytical data.
There are a number of redirect plug-ins for WordPress but none of them worked for me. It’s also nice to keep your WordPress installation as light as possible, so I went looking for the cleanest possible solution which turned out to be directly modifying the .htaccess file.
How to redirect images to posts in WordPress
- Create or edit the .htaccess file in the root of your WordPress installation, usually done via FTP.
- Add and customize the following line of code to suit your needs:
RedirectPermanent /wp-content/uploads/image.gif http://www.website.com/path/ - Save the .htaccess file to the root folder on your WordPress installation and test by trying to access the image URL directly.