2

My images were all held in the app itself and so were referenced with /images/12345.jpg which in some places has been hard coded into the content of the cms, with or without the full url.

The images have now been moved to s3 and so I want to add a redirect for urls that are of the following formats: /images/12345.jpg|png|gif or http://www.example.com/images/12345.jpg|png|gif (but only for only numeric filenames)

and point them to

http://my.images.images.s3.amazonaws.com/540x310/12345.jpg|png|gif

I currently have

use Rack::Rewrite do
rewrite %r{images\/(\d*.)(jpe?g|png|gif)$}, 'http://my.images.images.s3.amazonaws.com/540x310/$1$2'
end

But this doesn't seem quite right.

UPDATE------ of course the URL in the source doesn't change, I should have realised that. Clicking the link directly returns an error:

No route matches [GET] "/http://my.images.images.s3.amazonaws.com/540x310/13135.jpeg"

(note the leading slash)

So the rewrite is working but it thinks it should be routing to an internal link not an external URL.

UPDATE2------ Changing to

r302 %r{(?:images\/)(\d*.jpe?g|png|gif)$}, "http://my.images.images.s3.amazonaws.com/540x310/$1"

will now redirect if I go to http://www.example.com/images/13135.jpeg

but is not rendering the images in the site itself.

Cœur
  • 37,241
  • 25
  • 195
  • 267
dp6ai
  • 41
  • 3
  • "But this doesn't seem quite right" what does that mean? Are you getting errors or what? – phoet Nov 29 '13 at 12:17
  • no error messages, no redirect, nada. the regex is correct i'm pretty sure. – dp6ai Nov 29 '13 at 14:01
  • made a slight correction to the regex,but still not matching... (?:images\/)(\d*.jpe?g|png|gif)$ – dp6ai Nov 29 '13 at 14:07
  • test your redirects with curl like `curl -I "http://localhost:5000/images/12345.jpg"` – phoet Nov 29 '13 at 15:01
  • with rewrite i HTTP/1.1 404 Not Found with r302 i HTTP/1.1 302 Found with the redirected location – dp6ai Nov 29 '13 at 15:10
  • yeah, rewrite won't work with a foreign host. redirect should work though. – phoet Nov 29 '13 at 15:16
  • so a 302 as dexcribed shoul be showing the images correctly inline? The old URL is showing in the source but if i load that in a new broswer it does the right thing. Not sure how i can get the images to display properly? Any ideas? – dp6ai Nov 29 '13 at 16:09
  • i think so and SO also says so: http://stackoverflow.com/questions/3778347/is-it-ok-to-http-redirect-images – phoet Nov 29 '13 at 16:18
  • another thing you can do is serve the images from your rails app by fetching it from the remote and caching them on disc or something like that. – phoet Nov 29 '13 at 16:19
  • the r302 is redirecting fine with curl but looking at the network tab in chrome and firefox i'm getting 404 Not Found. Any idea why the browser would ignore it? – dp6ai Nov 29 '13 at 17:04
  • sure that it redirects to the right file? did you try opening the original url in the browser? – phoet Nov 30 '13 at 14:26
  • yes, if i put the original path in my browser it redirects to the new location. localhost:3000/images/13135.jpeg >> http://my.images.s3.amazonaws.com/540x310/13135.jpeg – dp6ai Nov 30 '13 at 17:53
  • this was caused by the url on the localhost still pointing to the live url and therefore the 301 rewrite never being called. fixed now. thanks for your help phoet – dp6ai Dec 02 '13 at 15:31

1 Answers1

2

this was caused by the url on the localhost still pointing to the live url and therefore the 301 rewrite never being called.

So the local deployment had the live URL hardcoded into the page, i was then changing this and pointing my browser at localhost.

In the page though the old URL remained which meant the page was looking to the live server for a possible redirect which had never been dpeloyed

dp6ai
  • 41
  • 3