# Test if an external url is valid before redirecting to it?

**URL:** <https://www.ruby-forum.com/t/test-if-an-external-url-is-valid-before-redirecting-to-it/159980>\
**Category:** Rails\
**Created:** [February 10, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/test-if-an-external-url-is-valid-before-redirecting-to-it/159980 "2009-02-10T17:56:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 10, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/test-if-an-external-url-is-valid-before-redirecting-to-it/159980/1 "2009-02-10T17:56:12Z")

</div>

In our site, users can have a url associated with their account to which  
they are redirected when their login expires. This can differ from user  
to user, and is prone to being mis-copied, or the sites in question not  
being there any more etc. I’d like to test if the url is valid before  
sending the user on to it.

So, i want, in the controller, to do something like this -

if external\_url\_is\_valid?(user.expiry\_url)  
redirect\_to user.expiry\_url  
else  
redirect\_to “our\_default\_expiry\_page”  
end

Can anyone tell me a nice, simple and efficient way of doing the  
“external\_url\_is\_valid?” bit?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 10, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/test-if-an-external-url-is-valid-before-redirecting-to-it/159980/2 "2009-02-10T18:40:33Z")

</div>

On Tue, Feb 10, 2009 at 6:56 PM, Max W. \<  
removed\_email\_address@domain.invalid\> wrote:

> redirect\_to user.expiry\_url

require ‘net/http’  
require ‘uri’

def external\_url\_is\_valid?(url)  
uri = URI.parse(url)  
response = Net::HTTP.start(uri.host, uri.port) {|http|  
http.head(uri.path)}  
response.is\_a?(Net::HTTPSuccess) ||  
response.is\_a?(Net::HTTPRedirection)  
end

Watch the validity of the url - [www.google.com](http://www.google.com) is not valid  
[http://www.google.com](http://www.google.com) also won’t work, [http://www.google.com/](http://www.google.com/) will

I wouldn’t do this at the point of redirection, I would do it at the  
point  
of saving the redirection url

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 10, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/test-if-an-external-url-is-valid-before-redirecting-to-it/159980/3 "2009-02-10T18:52:26Z")

</div>

That’s perfect, thanks Andrew.

You’re probably right about testing the validity at the point of adding  
it rather than (or as well as) the redirect. I think i’ll put a remote  
test button next to the input field, and maybe call it automatically on  
change of the field.

Anyway, thanks again.  
max
