views:

70

answers:

3

Some setup:

We have some static images, publicly available. However, we want to be able to reference these images with alternative URLs in the image tag. So, for example, we have an image with a URL like:

http://server.com/images/2/1/account_number/public/assets/images/my_cool_image.jpg

And, we want to insert that into our front html as:

<img src="http://server.com/image/2/my_cool_image.jpg"&gt;

instead of

<img src="http://server.com/images/2/1/account_number/public/assets/images/my_cool_image.jpg"&gt;

One really neat solution we've come up with is to use 301 redirects. Now, our testing has rendered some pretty neat results (all current generation browsers work), but I am wondering if there are caveats to this approach that I may be missing.

EDIT: To clarify, the reason we want to use this approach is that we are also planning on using an external host to serve up resources, and we want to be able to turn this off on occasion. So, perhaps the URL in the would be

http://client.com/image/3/cool_image.jpg

in addition to the "default" way of accessing

A: 

The only real downside is a second DNS lookup and a little server overhead to calculate the redirect, which both affect performance. Otherwise I can't think of any problem with this technique.

Gabriel Hurley
Um, the client has to make another round-trip to the server? Pretty big problem right there.
annakata
+6  A: 

Another technique that doesn't require a server round-trip is to use your HTTP server's equivalent of a "rewrite engine". This allows you to specify, in the server configuration, that requests for one URL should be satisfied by sending the results for some other URL instead. This is more efficient than telling the browser to "no, go look over there".

Greg Hewgill
A: 

One possible downside may be SEO - a long and complex file name with generic folder names may hinder a shorter/snappier URL.

The main issue though is that there will be an extra HTTP request for every image. In general you should try and minimise HTTP requests to improve performance. I think you should rewrite the URLs to the longer versions behind the scenes as others have said.

You don't have to rewrite the entire URL with the final URL. If you want a little more flexibility, you could rewrite the images like this:

http://server.com/image/2/my_cool_image.jpg

Rewritten as:

http://server.com/getImage?id=2&amp;name=my_cool_image.jpg

Then the "getImage" script would read a config file either serve up the longer-names file on server.com or the other one from client.com. You only have one trip to the server and a tiny bit of overhead on the server itself, unnoticeable to the visitor.

DisgruntledGoat