ASP.NET Friendly URLs and default.aspx - asp.net

I've implemented Friendly URLs in a Webform project but it allows a user to go to either the root of the website or /default. I really want to get rid of the /default option by having it always just route to the root. I've tried using
routes.MapPageRoute("", "Default", "~/")
but that doesn't seem to effect anything.
Anyone now the proper way to accomplish this?

The easiest way to achieve this will be through IIS not in code. It will create a 301 redirect from requests to default.aspx to the root.
<system.webServer>
<rewrite>
<rules>
<rule name="default page" stopProcessing="true">
<match url="^default\.aspx$" />
<action type="Redirect" url="{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Related

URL redirect not working in web.config

I've moved my blog to a different domain, and am trying to set up some redirect rules to redirect traffic. I have added the following to the system.webServer section of my web.config...
<rewrite>
<rules>
<rule name="URL1" stopProcessing="true">
<match url="oldurl.aspx" ignoreCase="true" />
<action type="Redirect" url="http://newdomain.com/newurl" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
...but it doesn't redirect.
I've tried using URLs of the following forms in the url attribute of the <action> tag, but it doesn't make any difference...
^/oldurl.aspx
/oldurl.aspx
http://olddomain.com/oldurl.aspx
Anyone any ideas what I'm doing wrong? As far as I can see, I've done the same as all the blog posts and SO answers suggest.
Not sure if it's relevant, but the old blog used dasBlog.

Asp.Net on IIS - Add default route to url

I am trying to change the default URL of my Asp.Net website on IIS. At the moment the URL leading to the default index.html, is just http://myservername/. But I would actually prefer to extend the URL to http://myservername/route1, which then still routes to the default html. How can I achieve this?
Download Microsoft URL Rewrite Module and install it
and add this to your web.config
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/route1" />
</rule>
</rules>
</rewrite>
</system.webServer>

iis redirect subdomain to subfolder on same subdomain

I have an IIS site and Im trying to use ReWrite 2.0 to redirect a particular subdomain to a sub folder. Within my IIS site I have it binded to two different domains:
one.example.com
two.example.com
When people visit one.example.com I want it to do nothing. When people visit http://two.example.com I want them to be redirected to http://two.example.com/subfolder.
Thanks for your help.
You need to add a match condition for the HTTP_HOST
<system.webServer>
<rewrite>
<rules>
<rule name="two.example.com Redirect" stopProcessing="false">
<match url="^\/?$" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*two\.example\.com.*" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://two.example.com/subfolder/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Here's a decent overall reference for the module:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
A very late answer, but hopefully it helps someone.
I presume you are using II7 or higher ?
IIS has a simple HTTP redirect function which is available per site listed in IIS.
Make sure you are in features view. Click on the site you want to redirect (In your case its http://two.example.com)
You should see this. Double click on HTTP Redirect
The you should see this. Enter your Redirect URL here (In your case its http://two.example.com/subfolder)
This existing question should solve your issue
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/menu_1/MainScreen.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>

Rewrite Subfolder to Subdomain in web.config

I'm attempting to write a rewrite rule for the following scenario.
User attempts to load this picture:
domain.com/images/folder/picture.jpg
and instead, I need it to load:
cdn.domain.com/images/folder/picture.jpg.
Here's what I have that isn't working:
<rule name="CDN rewrite for Images">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
<add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
</conditions>
<action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>
UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...
UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.
EDIT:
To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.
To do so:
Download and install the module
Open your IIS management console (inetmgr)
Select Your server node
Double click on Application Request Routing Cache:
Click on Server Proxy Settings on the Actions pane (right of the screen)
Check the box Enable proxy and click on Apply
The second step is about setting up your rules.
If you want your rewrite to be based on the path then use the following code:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
</rule>
</rules>
</rewrite>
Or if you keep the same folder architecture on the second website you can simplify as follow:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
If you want to catch only the files ending with a specific extension (let's say images):
<rewrite>
<rules>
<rule name="Forward to cdn domain">
<match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool
NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.
<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>

iis7 redirect from exact url to new site

I want to re-direct from an exact URL request to a new site within that domain.
I need to re-direct requests from
www.example.com/site111/default.aspx?configX.xml
(this site uses a different config file to load content)
to
www.example.com/site222/default.aspx
Issue is that I need to leave access www.example.com.au/site111/default.aspx
so I cannot just to a http re-direct as there is no file called default.aspx?configX.xml
I think I need to use a URL rewrite re-direct
Can someone help me with the syntax for the web config I cannot get it to work
0 module installed and have used the above syntax and it still doesn't work,
If I use the test feature on the module it says that the string matches,
very confused,
the actual webconfig section is here
<rewrite>
<rules>
<rule name="Redirect2" stopProcessing="true">
<match url="\/nrmmaps\/default.aspx\?config=nrmproject.xml$" />
<action type="Redirect" url="/nrmproject/default.aspx" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
and the old link is
http://se.nrmspace.com.au/nrmmaps/default.aspx?config=nrmproject.xml
and I want it to re-direct to
http://se.nrmspace.com.au/nrmproject/default.aspx
Do you have any idea why this is not working?
I believe you need to install the IIS rewrite module which can be found here:
http://www.iis.net/downloads/microsoft/url-rewrite
After you do that, you can add a rewrite section with a redirect rule in your web.config like so:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="\/site111\/default.aspx\?configX.xml$" />
<action type="Redirect" url="/site222/default.aspx" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
Hope this helps

Resources