Changing the URL Extension - asp.net

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

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.

IIS url rewrite, broken href (css, js etc)

I am using IIS url rewriting module in asp.net application, my problem is any internal relative references like js, css, images are now pointing to wrong url after this url rewrite, below is my rewriting rule
<rewrite>
<rules>
<rule name="pk" patternSyntax="ECMAScript">
<match url="pk/([a-z]+).aspx" />
<action type="Rewrite" url="{R:1}.aspx?mid=1" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="(\.css|\.js)$" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
In above rewrite any url with "pk/page_name.aspx" is being translated to page_name.aspx?mid=1, this is working fine, however references to js,css and images in master page are now also pointing to "pk/files/js/jquery.js" while in my application it should be "files/js/jquery.js", Please help me solve this issue.
I am able to solve this issue by myself, Earlier i was doing it completely wrong, I was doing just a rewrite which is causing problem, I solved this by first "Redirecting" the page to the desired URL and then "Rewriting" that URL to the one which my application understand. Following are my configuration
<rewrite>
<rules>
<rule name="Redriect for Markets" stopProcessing="true">
<match url="([a-z]+)\.aspx" />
<action type="Redirect" url="/{id:{C:1}}/{R:1}.aspx" appendQueryString="false" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="(\.css|\.js|\.jpg|\.png|\.woff|\.tiff|\.gif|\.dev|\.swf)$" negate="true" />
<add input="{QUERY_STRING}" pattern="mid=(.+)" />
</conditions>
</rule>
<rule name="Rewrite for Markets" stopProcessing="true">
<match url="([a-z][a-z])/(.*)" />
<action type="Rewrite" url="{R:2}?mid={marketId:{R:1}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="(\.css|\.js|\.jpg|\.png|\.woff|\.tiff|\.gif|\.dev|\.swf)$" negate="true" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="id">
<add key="1" value="pk" />
</rewriteMap>
<rewriteMap name="marketId">
<add key="pk" value="1" />
</rewriteMap>
</rewriteMaps>
</rewrite>
After doing this my URL which is like https://www.mydomain.com/index.aspx?mid=1 is first "Redirected" to https://www.mydomain.com/pk/index.aspx and then "Rewritten" to https://www.mydomain.com/index.aspx?mid=1 internally. So my browser window show URL as https://www.mydomain.com/pk/index.aspx and value I get in Request.QueryString["mid"] is 1, which is exactly what i wanted.
One thing more for internal URLs to work I have to take the base URL in master page currently it is set to "/"

Adding ignore case in URL rewriting in 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>

URL rewriting in ASP.NET turning http://example.com/abcd into http://example.com/page.aspx?id=abcd

I have a problem in my web forms ASP.NET app where I would like to rewrite a url from
http://example.com/abcd
into
http://example.com/page.aspx?id=abcd
the abcd part will be unique and I cant create a folder for it
I want the users to always see the http://example.com/abcd url
would the solution be the same in Windows Azure?
Can somebody please help me with some hints?
Thank You!
In your web.config, in the system.webServer section put something like this:
<!-- This has been added to support url rewriting for ... -->
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Page\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id=([a-zA-Z]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([a-zA-Z]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Page.aspx?id={R:1}" />
</rule>
</rules>
</rewrite>
Please note that I have copied from the config we have in a production site and modified a little bit... needs testing.
The config should work when you have only words ([a-zA-Z]+), change the pattern to make it work for numbers.
hope it helps

Resources