1) URL を標準形式に変換する
私の現在のプロジェクトでは、それを行うためにアドレス指定可能な宝石を使用しています:
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) URL を比較します。 canonical_url1 == canonical_url2
更新:
Does it work with sub-domains?
-いいえ、つまり、私たちはそれを言うことはできず、translate.google.com
平等google.com
です。もちろん、必要に応じて変更できます。