Create a rule with Service.Transfer() Behaviour IIS Rewriting Module - asp.net

Is it possible to create a rule that has a behaviour similar to the Service.Transfer from ASP ?

Using IIS7 Rewriting Module you can use a Rewrite Action.
This keeps the original URL but rewrites the path which your application will see and process in a similar way to Server.Transfer.
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
Have a look at the rules overview.
This essentially is the same as Context.RewritePath.

Related

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>

Can I make this web.config rewrite rule?

In web.config I would like to set up the following rule for my ASP.NET project:
A request to:
mydomain.com/123/some-page
returns page
mydomain.com/collection/123-some-page.html
In other words
domain/x/y should fetch and return domain/collection/x-y.html
The visitor should still see the domain/x/y in the browser url.
Can I make such a rewrite rule?
Remember to install url rewrite module in IIS
and set below rule in web.config
<rewrite>
<rules>
<rule name="My rewrite">
<match url="^(.*)/(.*)$" />
<action type="Rewrite" url="collection/{R:1}-{R:2}.html" />
</rule>
</rules>
</rewrite>

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

calling urlrewrite from code

I have a url as a string and I want to call the asp.net code (IIS7 module) that rewrites urls on their way into my application and get a rewritten url from it:
http://webserver/nice/url/youHaveThere
to
http://webserver/app/Default.aspx?category=nice&catalog=url&pageid=youHaveThere
I want to call this function from inside an asp.net application. How do I do it?
I'm assuming you have the module already installed on your server, and are just asking about defining a specific rule. This is where you can read everything you need about creating rules.
The rules are all done through regular expressions, not my forte. But this is a format example:
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
The ([0-9]+) stands for number only patterns, ([_0-9a-z-]+) stands for text and number. I would think this would work for what you're tying to:
<rewrite>
<rules>
<rule name="Rewrite to Default.aspx">
<match url="^article/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="app/Default.aspx?category={R:1}&catalog={R:2}&pageid={R:3}" />
</rule>
</rules>
</rewrite>
I wouldn't think you'd call a "rewrite rule". Instead you'd just use a Regex Replace. Maybe something like:
string nice_url = "http://webserver/nice/url/youHaveThere"
string new_url = Regex.Replace(nice_url, #"/(?<category>\w+)/(?<catalog>\w+)/(?<pageid>\w+)$", "app/Default.aspx?category=${category}&catalog=${catalog}&pageid=${pageid}");

Resources