Alex Rivera | Logout

Check if 2 URLs are equal

Asked 2009-08-03T14:16:37.250
22

Is there a method that tests if 2 URLs are equal, ie point to the same place? I am not talking about 2 URLs with different domain names pointing to the same IP address but for example, 2 URLs that point to the same .aspx page:

is equal to these:

Note/assumtions

  1. QueryString Values are Ignored
  2. ASP.NET (Pref C#)
  3. Default.aspx is the default page

----UPDATE----

This is a very crude method that tests a URL to see if matches the current URL: I tried creating a new Uri() with both the local and check URLs but dont know that works and went down the string checking avenue. The implementation of the SiteMapProvider skips this step if the URL starts with "HTTP" as this assumes an external URL. Since I have a SaaS framework that will always ensure relative paths (as these can be on different subdomains) it easier to strip things down.

Any comments on optimization? I guess for a start we can pass in a variable containing the current URL? Not sure of the overhead of calling HttpContext.Current.Request.Url.LocalPath many times?

/// <summary>
/// Assumes URL is relative aspx page or folder path
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool CurrentURLMatch(string url)
{
    string localURL = HttpContext.Current.Reques
Edit
Report

1 Answer

11

for the record, here is the translation of http://en.wikipedia.org/wiki/URL%5Fnormalization to C#:

using System;
using System.Web;

namespace UrlNormalizationTest
{
    public static class UrlNormalization
    {
        public static bool AreTheSameUrls(this string url1, string url2)
        {
            url1 = url1.NormalizeUrl();
            url2 = url2.NormalizeUrl();
            return url1.Equals(url2);
        }

        public static bool AreTheSameUrls(this Uri uri1, Uri uri2)
        {
            var url1 = uri1.NormalizeUrl();
            var url2 = uri2.NormalizeUrl();
            return url1.Equals(url2);
        }

        public static string[] DefaultDirectoryIndexes = new[]
            {
                "default.asp",
                "default.aspx",
                "index.htm",
                "index.html",
                "index.php"
            };

        public static string NormalizeUrl(this Uri uri)
        {
            var url = urlToLower(uri);
            url = limitProtocols(url);
            url = removeDefaultDirectoryIndexes(url);
            url = removeTheFragment(url);
            url = removeDuplicateSlashes(url);
            url = addWww(url);
            url = removeFeedburnerPart(url);
            return removeTrailingSlashAndEmptyQuery(url);
        }

        public static string NormalizeUrl(this string url)
        {
            return NormalizeUrl(new Uri(url));
        }

        private static string removeFeedburnerPart(string url)
        {
            var idx = url.IndexOf("utm_source=", StringComparison.Ordinal);
            return idx == -1 ? url : url.Substring(0, idx - 1);
        }

        private static string addWww(string url)
        {
            if (new Uri(url).Host.Split('.').Length == 2 && !url.Contains("://www."))
            {
               return url.Replace("://", "://www.");
            }
            return u
answered 2013-02-20T10:41:14.930

Your Answer