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

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

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

URL Rewrite in IIS 7 causes redirect loop

I have tried various methods found on the web (including some SO answers) for getting URL rewrite in IIS 7 to work so that I can turn mysite.com/somepage.aspx into mysite.com/somepage, for example. The last thing I've tried is the video at this link: https://www.youtube.com/watch?v=bBNJE7XA1m0. After applying these changes in IIS, I now can request mysite.com/somepage and get to mysite.com/somepage.aspx with the .aspx removed in the address bar. Partial success.
When I try to directly request mysite.com/somepage.aspx, however, I get into a redirect loop. I am hoping there is some simple mistake in my settings. Here is the web.config section created by making changes in IIS:
<rewrite>
<rules>
<rule name="HideAspxExtension">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny">
<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:0}.aspx" />
</rule>
<rule name="RedirectingAspxExtension" stopProcessing="true">
<match url="^(.*).aspx$" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="^(.*).aspx$" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
I have tried to apply this setting to multiple applications and I get the same results. What I do not have is another server to test on.
These are rules I've used before:
<rule name="StripAspx">
<match url="^(.+)\.aspx$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Rewrite the .aspx for internal processing-->
<rule name="RewriteASPX" 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" />
</rule>
The main differences I can see are:
These have the removal of the .aspx first (in rewrite rules, order matters). This also means that in these rules the stopProcessing directive is on the rewrite to .aspx, not the redirect away from it.
These don't have <add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />

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

redirect issue on webconfig file

please check sfveinaesthetics.com/index.php opens but http://sfveinaesthetics.com/ dont.. its a wordpress site in iis7 server.
inner pages work http://sfveinaesthetics.com/about-us/ fine..
Please get a me solution asap.. web config is like
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" 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="index.php/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Not sure if it's causing the problem but the default rewrite rule for Wordpress does not include the /{R:0} part in the rewrite action. Normally it's:
<action type="Rewrite" url="index.php" />

An alternative way to rewriting URLs than the Rewrite URL graphic interface in IIS 7?

Is there any way to do it, without the user interface?
I mean a non user interface way, something like a config file hosted in my web site, or some kind of alternative ?
Thank you very much!
YES, absolutely.
Your URL Rewrite rules should be stored in web.config file (unless it is explicitly forbidden which means they will be stored in IIS main config file -- for security and performance purposes, I guess).
Here is an example of such web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="SeeOther" />
</rule>
<rule name="CatchAll" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/catchall.php?page={REQUEST_URI}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources