0

I want to redirect the following URL (and many, many more like it on my web app):

http://0.0.0.0:3000/images/q/big_background/1680x1008/quality/83/interlace/true/src/http%3A%2F%2F0.0.0.0%3A3000%2Fmedia%2FW1siZiIsIjIwMTQvMDIvMDYvdjFiZzFleXRkX3NsaWRlc2hvd19maWxsLmpwZyJdXQ%2Fslideshow_fill.jpg

This is the route I am using:

match 'images/(*path)' => redirect { |params, request| "http://0.0.0.0:8080/#{params[:path]}" }

I expect that the above url would be redirected to a new server as (notice that everything after "src" is still url encoded and that there is a .jpg):

http://0.0.0.0:8080/q/big_background/1680x1008/quality/83/interlace/true/src/http%3A%2F%2F0.0.0.0%3A3000%2Fmedia%2FW1siZiIsIjIwMTQvMDIvMDYvdjFiZzFleXRkX3NsaWRlc2hvd19maWxsLmpwZyJdXQ%2Fslideshow_fill.jpg

However, right now it is being sent as (notice that everything after src is no longer encoded and that the .jpg has been truncated):

http://0.0.0.0:8080/q/big_background/1680x1008/quality/83/interlace/true/src/http://0.0.0.0:3000/media/W1siZiIsIjIwMTQvMDIvMDYvdjFiZzFleXRkX3NsaWRlc2hvd19maWxsLmpwZyJdXQ/slideshow_fill

This is happening during the routing. But I have no idea why the routes would decode the url and truncate. A puts of params[:path] shows that is it truncated at that point, so it must be the (*path) but I am not sure how to get the redirect to work properly. Any help would be greatly appreciated!

Tim C
  • 1,934
  • 12
  • 25
  • I'm not sure what's causing the truncation, but is there a particular reason you're handling this sort of redirection in Rails? Most set-ups I've seen tend to do this sort of work at the public-facing HTTP server side (Apache, nginx, et al.) to bypass the overhead of using Rails. – Aupajo Feb 06 '14 at 23:30
  • You make an good point, I will look into that one :) ultimately I would like to re-architect this entire solution to not use an image processing server in this way. But for tonight, I'd love to figure out why the redirect un-encodes and truncates things. – Tim C Feb 06 '14 at 23:35

1 Answers1

0

Rails would do some escape task on route automatically, it seems impossible to achieve with using route only...

If you really want to do this, create a controller and get the url with request.original_url, then redirect it.

Ref: stackoverflow - How do I get the current absolute URL in Ruby on Rails?

Community
  • 1
  • 1
Sibevin Wang
  • 4,480
  • 3
  • 27
  • 27