jtrupiano / rack-rewrite

A web server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules.
http://github.com/jtrupiano/rack-rewrite
MIT License
836 stars 80 forks source link

redirect hardcoded image urls #52

Closed dp6ai closed 10 years ago

dp6ai commented 10 years ago

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 http://www.mysite.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, any pointers would be a great help.

thanks

dp

--------------------------UPDATE--------------------------

this seems to be working, almost

require 'rack/rewrite'

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

curl -I 'localhost:3000/images/13135.jpeg' i get HTTP/1.1 301 Moved Permanently and the correct location but the browser (chrome and FF, cache cleared) are showing 404 Not Found.

for this source:

i assume the source should be displaying the original URL? Any idea why it wouldn't render the image if the redirect is correct?

dp6ai commented 10 years ago

This is sorted. The hard coded URL's were pointing to the live server where the rewrite has not yet been commited.