Extensionless URLS - IIS Rewrite

Redirecting ASP.NET Legacy URLs to Extensionless with the IIS Rewrite Module ASP.NET has included support for "friendly URLs" for a while now. ASP.NET MVC has always supported friendly URLs and more recently, so has Web Forms. That means if you don't want to have the .aspx extension, you certainly don't have to.

There are a LOT of existing legacy apps out there, as well as apps that you may not have full control over. For example, there's a site that I want to influence but it's got dozens (hundreds) of links to foo.html and bar.html existing pages.

I want to:

  1. Redirect existing GETs to a /foo.html to /foo
  2. AND keep rewriting /foo to the underlying /foo.html so it handles the request
All rewrites are internal
You can do all this within the web.config using the IIS Url Rewrite Module. You can do this with ANY file type that IIS can handle, meaning this isn't an ASP.NET-specific thing. This all happens well before your application gets involved.

Here's what the web.config looks like. Note that since the example runs in just IIS 7+ and the Rewrite module, you can just add this file (or add to your existing web.config). There was no configuration needed. The same would apply to any existing site. Be aware that sometimes super-"greedy" rewrite or redirect rules can end up grabbing ahold of your CSS or JS so you'll want to be aware if something odd happens. Also be aware that this is effectively a 302 redirect, and your form post will act like it's not working if you don't specify your form action appropriately (in my case, I had to post to the extensionless file name).

web.config


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
	<rule name="extensionless" stopProcessing="true">
		  <match url="(.*)\.html$" />
		  <action type="Redirect" url="{R:1}" redirectType="Permanent" />
	</rule>
	<rule name="removeextension" enabled="true">
		<match url=".*" negate="false" />
			<conditions>
				<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
				<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
				<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
				<!--Option instead of above line <add input="{URL}" pattern="\." negate="true" />-->
			</conditions>
			<action type="Rewrite" url="{R:0}.html" />
	</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Additionally, the IIS Rewrite module has a great UI for IIS that will write a lot of these rules for you and let you test them interactively.

IIS web server URL rewrite


Back To Top
© 1998 - 2026 psacake.com
Version 8.13 | Advertise on this site