1)转换网址canonical form
在我目前的项目我使用addressable宝石为了做到这一点:
def to_canonical(url)
uri = Addressable::URI.parse(url)
uri.scheme = "http" if uri.scheme.blank?
host = uri.host.sub(/\www\./, '') if uri.host.present?
path = (uri.path.present? && uri.host.blank?) ? uri.path.sub(/\www\./, '') : uri.path
uri.scheme.to_s + "://" + host.to_s + path.to_s
rescue Addressable::URI::InvalidURIError
nil
rescue URI::Error
nil
end
例如:
> to_canonical('www.example.com') => 'http://example.com'
> to_canonical('http://example.com') => 'http://example.com'
2)比较你的网址:canonical_url1 == canonical_url2
UPD:
Does it work with sub-domains?
- 不,我的意思是,我们不能说translate.google.com
和google.com
是相等的。当然,你可以根据你的需要修改它。
那么,什么是你想要的方式?它何时应该返回真实?只需检查控制器和操作? – 2012-08-01 13:56:06
@AnthonyAlberto我想我的原始问题是要求一个更通用的解决方案,它将采用任何两个URL(即使它们指的是外部站点上的页面),并检查它们是否引用同一页面。想想看,虽然只是检查控制器和行动实际上对我来说很好。 – Ajedi32 2012-08-01 14:36:05
但它是在你的应用程序的上下文吗?或者你需要测试任何网址? – 2012-08-01 14:40:44