URL Rewriting in ASP.NET web form using URL Rewrite module of IIS

Whenever we talk out SEO friendliness of any website first thing we should think about is the URLs. URLs play big role in any website’s SEO score. If you have SEO friendly URLs half of your work is done (don’t take it literally, I am just giving example).

So for creating SEO friendly URLs first thing we have to do is install URL Rewrite extension on our IIS. You can get one from here. You will find different option for downloading and installing extension. You can choose if you want to install using web platform installer or standalone installer. You will also get samples at the bottom of the page linked above.

IIS URL Rewrite module works quit similar to Apache Mod Rewrite and you will see if you are familiar with Mod Rewrite.

After installing extension just create an application on IIS. Open IIS Manager right click on Default Website and click add application. Here you have to give an alias name and select physical location of you application on hard drive. Click Ok.

Note: After adding application if connection panel is not showing your application then right click on Default website and select refresh.

Now you have to add a web.config file on root of you application if not there already. Now you can add your rules in web config file.

We will start with a basic rule. Which will work if you want to make you ASP.NET web form application URLs to look like MVC URL. For this we will add this rule:

<configuration>
	<system.webServer>
		<rewrite>
			<rules>
				<rule name="MVC Basic" stopProcessing="true">
					<match url="/(.*)/(.*)/(.*)" />
					<conditions logicalGrouping="MatchAll">
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
					</conditions>
					<action type="Rewrite" url="{R:1}.aspx?id={R:2}&amp;name={R:3}" />
				</rule>
			</rules>
		</rewrite>
	</system.webServer>
</configuration>

After adding this code in web.config if you will open below URL:

http://localhost/YourApplicationName/student/111/John

This will be converted to:

http://localhost/YourApplicationName/student.aspx?id=111&name=John

So your request will ultimately go to your page name as first parameter. In above example it is student to request will go to student.aspx and rest of the parameters will be passed as query string parameter id and name.

You may notice that we have used a tag named conditions in rule tag. This is generally used to check any precondition before applying our defined rule and action. In above example we have added two condition which works like below:

  • First condition checks if request URL is a file on specified path and we have added negate to true so if there is no file with given path then only our rule will be applied.
  • First condition checks if request URL is a folder on specified path and we have added negate to true so if there is no folder with given path then only our rule will be applied.

And the attribute named logicalGrouping in conditions tag given option to specify that if all the condition defined are true then perform this action. We can also specify if any one of the given condition is true then it will perform an operation.

In our example, we have set logicalGrouping to MatchAll for allowing action only when all conditions are true. If we want to do any action when any one condition is true just set it to MatchAny.

So this was a basic use and implementation of URL Rewriting module in ASP.NET web form application.

Leave a Reply

Your email address will not be published.