test if an external url is valid before redirecting to it?

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 is not valid http://www.google.com also won’t work, 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 Timberlake http://ramblingsonrails.com


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