Redirect to a 3rd party website

27 views
Skip to first unread message

Linus Pettersson

unread,
Jul 29, 2012, 1:09:41 PM7/29/12
to rubyonra...@googlegroups.com
Hi!

I need to redirect to some 3rd party websites. The issue is that some of the urls works to redirect and some doesn't work, using redirect_to url

Example 1:

With this URL I just do: redirect_to url

Example 2:

When using: redirect_to url
I get "ERROR URI::InvalidURIError: bad URI(is not URI?)"

So, I tried to just do:
redirect_to URI.encode(url)
... and it works! But then the URL in example 1 is not working anymore. Says the site is not found. No error in Rails though.


So, I need an approach that works with both (all?) URIs. Any ideas?

Hassan Schroeder

unread,
Jul 29, 2012, 1:30:58 PM7/29/12
to rubyonra...@googlegroups.com
On Sun, Jul 29, 2012 at 10:09 AM, Linus Pettersson
<linus.pe...@gmail.com> wrote:

> I need to redirect to some 3rd party websites. The issue is that some of the
> urls works to redirect and some doesn't work, using redirect_to url

> So, I need an approach that works with both (all?) URIs. Any ideas?

Write a utility method, e.g. redirect_to sanity_checked(url) that either
returns the original URL if it's good or an encoded version if not.

Which is not to say that simply encoding it will fix every error, so it's
probably worth checking that result too :-)

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Linus Pettersson

unread,
Jul 29, 2012, 1:44:48 PM7/29/12
to rubyonra...@googlegroups.com
Thank you for your time.

How can I check if the URL is "good" or needs to be encoded?

Best Regards
Linus


Den söndagen den 29:e juli 2012 kl. 19:30:58 UTC+2 skrev Hassan Schroeder:
On Sun, Jul 29, 2012 at 10:09 AM, Linus Pettersson  
> wrote:

> I need to redirect to some 3rd party websites. The issue is that some of the
> urls works to redirect and some doesn't work, using redirect_to url

> So, I need an approach that works with both (all?) URIs. Any ideas?

Write a utility method, e.g. redirect_to sanity_checked(url)  that either
returns the original URL if it's good or an encoded version if not.

Which is not to say that simply encoding it will fix every error, so it's
probably worth checking that result too  :-)

HTH,
--
Hassan Schroeder ----------------------

Linus Pettersson

unread,
Jul 29, 2012, 1:53:52 PM7/29/12
to rubyonra...@googlegroups.com
I mean, URI.parse(uri) doesn't return any error on either of the URIs it seems.

Linus Pettersson

unread,
Jul 29, 2012, 2:04:14 PM7/29/12
to rubyonra...@googlegroups.com
Actually, I think I found a solution now.

I first parse it, like:
uri = URI.parse(the_url)
and then:
redirect_to uri.to_s


Works :)


Den söndagen den 29:e juli 2012 kl. 19:30:58 UTC+2 skrev Hassan Schroeder:
On Sun, Jul 29, 2012 at 10:09 AM, Linus Pettersson
 wrote:

> I need to redirect to some 3rd party websites. The issue is that some of the
> urls works to redirect and some doesn't work, using redirect_to url

> So, I need an approach that works with both (all?) URIs. Any ideas?

Write a utility method, e.g. redirect_to sanity_checked(url)  that either
returns the original URL if it's good or an encoded version if not.

Which is not to say that simply encoding it will fix every error, so it's
probably worth checking that result too  :-)

HTH,
--
Hassan Schroeder ------------------------

Linus Pettersson

unread,
Jul 29, 2012, 2:18:23 PM7/29/12
to rubyonra...@googlegroups.com
My bad, it does NOT work!

Hassan Schroeder

unread,
Jul 29, 2012, 5:58:03 PM7/29/12
to rubyonra...@googlegroups.com
On Sun, Jul 29, 2012 at 10:53 AM, Linus Pettersson
<linus.pe...@gmail.com> wrote:
> I mean, URI.parse(uri) doesn't return any error on either of the URIs it
> seems.

Without looking at the code, it's hard to imagine you're getting the
"ERROR URI::InvalidURIError: bad URI(is not URI?)" exception you
originally reported from other than URI.parse, but -- can you get a
stack trace to show exactly what's raising that?
Reply all
Reply to author
Forward
0 new messages