How to redirect images in WordPress

By on Feb 20, 2012 in Blog Posts, Internet | 0 comments

Share On GoogleShare On FacebookShare On Twitter

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

  1. Create or edit the .htaccess file in the root of your WordPress installation, usually done via FTP.
  2. Add and customize the following line of code to suit your needs:
    RedirectPermanent /wp-content/uploads/image.gif http://www.website.com/path/
  3. Save the .htaccess file to the root folder on your WordPress installation and test by trying to access the image URL directly.
What you should see is an instant redirect to your post or website as defined in your RedirectPermanent code. This automatically points anyone coming in from the direct image URL to your defined path. It will break embeds on most sites that just pull your content directly into their site, but I don’t think most webmasters will mind that since the whole point of having your own site is to get people to visit it.