Adding ignore case in URL rewriting in ASP.Net - asp.net

I am using following web.config entry for url rewriting to remove .aspx extension.
<rewrite url="~/(.+)" to="~/$1.aspx" />
The problem I am getting here is if I have any image on page, it assigns .aspx extension to image.
Also if I tried to access my site like http://exmaple.com, it get redirected to http://exmaple.com/default.aspx.aspx.
I want to know if there is any way to add ignore case in web.config.

Your rewrite should look something like this, to remove .aspx
<rewrite>
<rules>
<rule name="RewriteASPX">
<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" />
</rule>
</rules>
</rewrite>

Related

Webconfig URL Rewrite to Hide Classic ASP ext but not .aspx or other ext

I have been able to hide the .asp extension, but it also removes any other extensions and points to .asp
I know this is just a config issue but have no experience of the webconfig file configuration and wonder if anyone has a quick solution to save me a few hrs!
Code as I have it below from part of webconfig file
<rewrite>
<rules>
<rule name="Hide .asp Ext">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.asp" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.asp" logRewrittenUrl="true" />
</rule>
<rule name="Redirect .asp Ext" stopProcessing="true">
<match url="^(.*).asp" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).asp" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
Seems to me the first rule converts everything to an .asp extension first?
Try removing the first rule.
Try this? Seems to work on my test server.
<rule name ="redirect .asp to none" stopProcessing="true">
<match url="(.*)\.asp$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .asp extension" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" />
</rule>
You have two rules defined. It looks like the first matches on all requests and "rewrites" them to have a .asp extension. The second matches on on .asp and then performs a redirect of some kind.
What is not clear from your question is how you want to 'hide' these files. If you want to deny all requests to .asp you would be much better served adding a Request Filtering "File Extensions" https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/index

How to Hide any page extension like (.aspx and .asp and .php and .html) in asp.net IIS7

I have a requirement to hide any type of page extension in my site which have hosted 100 more page asp,html and aspx pages.
I want to hide all type page extension . I use following code which only works for .aspx. not work on .html & .asp page extension
<rule name="RemoveASPX" enabled="true" stopProcessing="true">
<match url="(.*)\.aspx" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="AddASPX" 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" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>
<match url="(.*)\.(aspx|php|html)" />
<action type="Rewrite" url="{R:0}.{R:1}" />
You can change regex of the match element so it matches on the other extensions.
You can (and should) use the routing mechanism provided by Asp.NET, in your specific case the MapPageRoute method as described here: http://msdn.microsoft.com/en-us/library/vstudio/dd329551%28v=vs.100%29.aspx
What you see about aspx pages can be applied to other kind of pages too, like plain html or old asp.

No .aspx page suffix, how come?

How come stackoverflow hasn't any .aspx page suffix in the url bar? Everything ends up in /something/ or /page
How can I achieve the same in asp.net?
Instead of webforms (and aspx pages) you could use MVC and it's routing feature (see http://www.asp.net/mvc/overview/controllers-and-routing) where a url is mapped to an "action" in a "controller".
A URL of /questions/25364620/whatever could be mapped to the "Index" action (a default value) of the "QuestionsController" ("questions" from the URL + "Controller"), with an id parameter of 25364620. The title is ignored.
These are called the friendly urls. I am not sure about how they have written in Stackoverflow but you can remove the .aspx entension by doing like this:
<rewrite>
<rules>
<rule name="RewriteMyASPX">
<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" />
</rule>
</rules>
</rewrite>

Changing the URL Extension

I had developed a Web application in Asp.net. When am using the Application over IIS, the file extension as.aspx is visible. Is it possible to hide it (or) rename it....Thankx in advance.
For .NET framework 4.0+, try the following rule in web.config (inside <system.webServer>):
The first will redirect URLs using the old format, to remove the .aspx extensions. You should of course update your links as well - eventually you won't need this.
The second rule rewrites URLs internally to add .aspx behind the scenes.
<rewrite>
<rules>
<rule name="RedirectOldFormat" enabled="true" stopProcessing="true">
<match url="(.*)\.aspx" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="InternallyAddAspx" 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" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>
</rules>
</rewrite>
For .NET Framework 3.5 or earlier, you can accomplish this using routing. Try the following solution:
http://www.codedigest.com/Articles/ASPNET/294_Search_Engine_Friendly_URLs_Using_Routing_in_ASPNet_35.aspx

Remove ASPX FILE Extention in Web.config But Negate Rewrite if URL Contains Period

All right, so by now most of us probably use the standard rule below for remove the aspx extention in your urls.
<rule name="Remove">
<!--Removes the .aspx extension for all pages.-->
<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" />
</rule>
However I would like to modify the rule to prevent the write rule from catching any url with a period in it.
That way if someone tries to type in http://www.domain.com/picture.jpg
The rule doesn't catch it.
Luckily the IsFile and IsDirectory conditions prevent actual files from being hit by the rule but whenever I have a case where someone types in a file that doesn't exist on the server then the rule catches it and asp.net does something like this:
http://www.domain.com/404error.aspx?aspxerrorpath=/picture.jpg.aspx
Id like it to not pass through the rule when there is a file not found.
Basically I just need to be able to add a condition that negates whenever a period is found in the url after the domain name. I'm assuming some sort of REGEX will work. I can't seem to get it working right though.
I was able to come up with the following solution.
<rule name="RewriteASPX" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>

Resources