Is it OK to HTTP redirect images?

Yes, you can redirect images and browsers will follow redirects. But you’ll generally want to keep redirection to a minimum for performance reasons, because each redirect requires a separate HTTP request, which adds server overhead and increases end-user page load time a little.

The one thing you should definitely avoid is redirecting many images on a page. This will severely slow down page load time, especially on high-latency networks (e.g. phone, China, satellite internet) where each new HTTP request takes a long time. Also, HTTP clients are limited to a small number of simultaneous HTTP connections per server hostname, so even on fast networks you’ll end up with a bottleneck.

Redirecting 1 or 2 images on a page is not a big deal, however.

If you redirect images and they’re cacheable, you’d ideally set an HTTP Expires header (and the appropriate Cache-Control header) for a date in the distant future, so at least on subsequent visits to the page users won’t have to go through the redirect again.

If the reason you’re redirecting is to conform to a new URL scheme, most web servers have an easy way to rewrite URLs on the server without having to send an actual redirect back to the client. In other words, the client may request /static/bar.jpg but the server can be configured to translate that into /media/images/bar.jpg. This URL-rewriting approach is preferable to redirection in most cases, since you can refactor where your content lives on the server without incurring the redirection overhead on the client or server side.

Leave a Comment