0

I know this can be done with URL Rewrite but this is a customer machine where they don't want to install extensions unless they really have to. The application is running in a folder under the original domain name like this:

domain.com/app1

If a request is made to domain.com/app1 I wan't it to be permanently redirected to domain.com/app1/.

Application and IIS:

  • .NET Framework 4.5
  • IIS 7.0

Example rule that is last resort:

<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • Out of curiosity, why? Under RFC 1738 slashes should only be used as a path component separator, as a terminator (your proposed use) they're meaningless: http://stackoverflow.com/questions/5948659/when-should-i-use-a-trailing-slash-in-my-url - what you're proposing would only add an extra HTTP redirect that would add latency to user-operations and ultimately harm the user-experience. – Dai Nov 17 '16 at 10:24
  • There is also the risk that a non-conforming UA (of which there are *many*) might truncate any trailing slashes in any request it sends-out and so get stuck in an infinite redirect loop. Please don't do this. – Dai Nov 17 '16 at 10:25
  • @Dai A very valid question. It is due to the current configuration of Angular that we are using. If a trailing slash is not added when the application is run from a sub directory, the angular routing will go into an infinite loop calling for a route that does not exist. Not ideal in any way but this is the situation. – Ogglas Nov 17 '16 at 10:30

1 Answers1

2

Solved it in Global.asax using this method:

protected void Application_BeginRequest()
{
    if (Request.ApplicationPath != "/" && Request.ApplicationPath.Equals(Request.Path, StringComparison.CurrentCultureIgnoreCase))
    {
        var redirectUrl = VirtualPathUtility.AppendTrailingSlash(Request.ApplicationPath);
        Response.RedirectPermanent(redirectUrl);
    }
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418