5

This question will work best with examples.

Say my web app is hosted at http://example.com/WebApp/

According to Microsoft, IIS will redirect if the trailing forward slash is missing. And this works as expected: a request sent to http://example.com/WebApp is redirected to http://example.com/WebApp/.

Now in my case, someone bookmarked the URL with an extra forward slash: http://example.com/WebApp//. This will load the web app as expected but now all the relative URLs are wrong. So if I call another app on the same domain, for example ../AnotherApp/SomePage.aspx, then it will try to load /WebApp/AnotherApp/SomePage.aspx instead of correctly loading /AnotherApp/SomePage.aspx.

How can I redirect http://example.com/WebApp// to http://example.com/WebApp/?

styfle
  • 16,664
  • 22
  • 73
  • 115
  • Not saying this is what you should do... or that it would work for sure as I can't test it right now. But perhaps you could add some code to the page load event that checks Request.Url then redirect the user to the page if the Url doesn't match. Have you tried anything? – Daniel Apr 17 '13 at 00:04
  • @DanielCook This won't work because at that point, the Request object says `/WebApp/Default.aspx` so it is too late. – styfle Apr 17 '13 at 18:05
  • possible duplicate of [Remove multiple forward slashes](http://stackoverflow.com/questions/12004223/remove-multiple-forward-slashes) – dana Apr 18 '13 at 15:48

2 Answers2

1

It appears difficult to do this with server side script alone. Perhaps doing a redirect in JavaScript would suffice?

<html>
<head>
<script type="text/javascript">
(function () {
    var protocol = location.href.substr(0, location.href.indexOf("://"));
    var restOfUrl = location.href.substr(location.href.indexOf("://") + "://".length);
    if (restOfUrl.match(/\/\//)) {
        location.href = protocol + "://" + restOfUrl.replace(/\/\//g, "/");
    }
})();
</script>
</head>
<body>
...
</body>
</html>

Also, you should consider setting a canonical link in your page so that search engines index things correctly.

dana
  • 14,964
  • 4
  • 53
  • 82
  • I was trying to avoid this solution. If no one comes up with a better answer, I'll accept this one. – styfle Apr 17 '13 at 23:56
  • I was having a tough time reading the 2 slashes on the server, so I resorted to this. Interestingly enough, there is almost the same question here - http://stackoverflow.com/questions/12004223/remove-multiple-forward-slashes. In the accepted answer, server variables are being read from within Global.asax.cs. Not sure if that works, but its worth a shot. – dana Apr 18 '13 at 05:46
  • I just tested and double slashes are present in the UNENCODED_URL server variable. You can check for that and redirect accordingly. – dana Apr 18 '13 at 15:53
  • It doesn't work for me. Request.ServerVariables["UNENCODED_URL"] is null. – styfle Apr 18 '13 at 16:39
0

You could use the IIS rewrite module (http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_Pattern) with a regex that redirects to the single slash version.

For example, you can match URL's with extra slashes with this regex:

http://.*/{2,}

Jason Young
  • 3,523
  • 3
  • 29
  • 38