How to redirect extra slash to URL using IIS

This question will work best with examples.

Say my web application is hosted on http://example.com/WebApp/

According to Microsoft , IIS will be redirected if missing a slash. And this works as expected: a request sent to http://example.com/WebAppis redirected to http://example.com/WebApp/.

Now in my case, someone added the URL-address with an extra slash: http://example.com/WebApp//. This will load the web application as expected, but now all relative URLs are erroneous. Therefore, if I call another application in the same domain, for example ../AnotherApp/SomePage.aspx, it will try to download /WebApp/AnotherApp/SomePage.aspxinstead of the correct download /AnotherApp/SomePage.aspx.

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

+5
source share
2 answers

It seems that this is difficult to do only with the server side of the script. Perhaps redirecting in JavaScript is enough?

<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>

In addition, you should consider setting up a canonical link on your page so that search engines index things correctly.

+1
source

You can use the IIS rewrite module ( http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_Pattern ) with a regular expression that redirects to one slash.

For example, you can match URLs with additional slashes to this regular expression:

http://.*/{2,}

0
source

All Articles