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