Redirecting from .aspx extension to Non .aspx

Currently in our website all the pages work with with and without .aspx extension and I was asked to redirect all the urls with .aspx extension to Non .aspx url (Friendly URL)

For example :

https://mysite/resources.aspx –> https://mysite/resources

This is a pretty simple request and can be implemented by writing IIS redirect rule. I just wanted to document this as this is a frequent request in multiple projects.

Please copy the below redirect rule inside the <rewrite> node in web.config file.

<rule name="Redirecting .aspx extension" stopProcessing="true">
      <match url="^(.*).aspx$" />
      <conditions logicalGrouping="MatchAny">
	   <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Redirect" url="{R:1}" />
</rule>

I did not want this rule to apply to files with .aspx extensions like showconfig.aspx, jobs.aspx. So I added a condition to not apply the rule to the files which end with .aspx extension.

<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

So this rule will work for all urls except to the urls with physical filenames with.aspx extension.

Thank you.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: