Extensionless URL

Description Article
Table of Contents
Operations
Remove extension using Url Rewrite Module on IIS 7
Remarks

An alternative pattern to be used for removing extension on URL can be:

^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-\.\/\(\)]+) where:

  • The “^” indicates the beginning of the line or string;
  • The parenthesis “()” allows to define a numbered capture group, meaning that its possible to reference everything inside the parens later by index. In this case 2 numbered capture exists. They are referred with the sintax {R:1} and {R:2};
  • The pattern [0-9a-zA-Z\-]+ indicates any character in 0-9, lowercase a-z, uppercase A-Z or “-”, one or more repetitions;
  • The / is a straight match to a slash “/”. In other words, the first named capture group will capture 0-9,a-z, A-Z and “-” prior to the first “/”. It can be referenced later with {R:1};
  • The pattern [0-9a-zA-Z\-\.\/\(\)] matches any character in 0-9, lowercase a-z, uppercase A-Z or “-”, “.”, “/”, “(“, “)”, one or more repetitions;
  • The rewrite URL is simply everything in the first named capture group (everything prior to the first /) which should be the filename (e.g. “TestService.svc”, without the extension
How to do it

Refer to this article for detailed description.

Remove extension on IIS 5.x/6.x using custom HTTP Handler
How to do it

Refer to this article for description of how to use an HTTP handler .

In order to use it to process extensionless URLs a specifci URL fragment must be configured in the handler configuration’s path property.

To handle requests like:

http://localhost/training/testservice1?sample=op1

a dedicated HTTP handler must be implemented and configured like:

<configuration>

	<system.web>

		<httpHandlers>
			<add verb="*" path="*/training/*" type="Rewriter2Handler"/>
		</httpHandlers>

	</system.web>

	<system.webServer>

		<handlers>
		      <add  verb="*" path="*/training/*"
                  name="Rewriter2Handler"
                  type="handlers"
                  modules="IsapiModule" scriptProcessor="C:\WINDOWS\Microsoft.NET\Frameworkv2.0.50727\aspnet_isapi.dll"/>
		</handlers>

	</system.webServer>

</configuration>
Remove extension using custom HTTP module
How to do it

Refer to this article for detailed description.

Remove extension without using "404.aspx" custom error page

A request with extensionless URL originates a HTTP 404 response. This means IIS receives the request and it serves the page configured for HTTP 404.

Generally this is a 404.html file that resides in IIS directory. IIS does not send the request to ASP.NET handler and therefore the request cannot be intercepted. If the request is forward to ASP.NET, the request with extensionless URL can be processed.

In order to forward HTTP 404 to ASP.NET ISAPI, all it is required is configure IIS to redirect to some “.aspx” page when a missing extensionless URL is hit.

How to do it

How to configure 404 redirection in IIS 5.x / 6.x:

The 404 error default page  must be changed to “404.aspx” and the type to “URL”.

How to configure 404 redirection in IIS 7:

iis7.png

The 404 error default page  must be changed to “/404.aspx” and the type to “ExecuteURL”. The default error response must also be changed to “Custom error pages“.

How to handle the 404 error

When a request with a URL like “http://localhost/training/TestService1” it will redirect to “localhost/404.aspx?404;http://localhost/training/TestService1“.

From Global.asax BeginRequestevent, the URL must be captured and inspected in order to see whether it’s an extensionless URL request. If it is, a customized rewrite analysis must me implemented:

protected void Application_BeginRequest(object sender, EventArgs e)
{
  string url = HttpContext.Current.Request.Url.AbsolutePath;

  // HTTP 404 redirection for extensionless URL or some missing file
  if (url.Contains("404.aspx"))
  {
    // On 404 redirection, query string contains the original URL in this format:
   // 404;http://localhost:80/Training/TestService1

   string[] urlInfo404 = Request.Url.Query.ToString().Split(';');
   if (urlInfo404.Length > 1)
   {
     string originalUrl = urlInfo404[1];

     string[] urlParts = originalUrl.Split('?');

     string queryString = string.Empty;
     string requestedFile = string.Empty;

     if (urlParts.Length > 1)
     {
       requestedFile = urlParts[0];
       queryString = urlParts[1];
     }
    else
    {
       requestedFile = urlParts[0];
    }

    if (requestedFile.IndexOf('.') > 0)
    {
       // There's some extension, so this is not an extensionless URL.
       // Don't handle such URL because these are really missing files
    }
    else
    {
       // Extensionless URL. Use URL rewriting logic to handle such URL
       HttpContext.Current.RewritePath(requestedFile +
					".svc?" + queryString);
    }
  }
}