Rewrite Subfolder to Subdomain in web.config - asp.net

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>

Related

IIS 7.0 URL REWRITE

I am setting up a redirect to WWW for one of our sites in the web.config and ran into a small issue. The code I have in the web.config for the rewrite is as follows :
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
I'm finding that it's actually working a little too well. Because of the pattern "example.com", I'm seeing that it's now redirecting to our live site on dev and staging because our URLS are laid out like so : dev.example.com & staging.example.com. For the time being, I have just commented out the rewrite on these other web.configs but I'm wondering if there's a better pattern or option to get around this issue.
If you only want the root domain without subdomains then you should edit your pattern in the HTTP_POST section.
Place a ^ in front of the pattern which means start with. So If the url starts with example.com then it gets redirected to www.example.com.
If its dev.example.com, this rule will be ignored.
Edit your example:
<add input="{HTTP_HOST}" pattern="^example.com" />

IIS 7.0 - URL Rewriting Root Folder

I have done a bunch of sites using URL Rewrite actions in the web.config with much success.
Today I am trying to do the following rewrite rule:
Example url: domainname.com/ANYTHINGHERE/PRODUCTID
I basically want to take the product ID and pass it to a page (i.e. product.aspx?id=PRODUCTID
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
I've done this in the past just fine but it had to have a static root folder, for instance: domanname.com/products/ANYTHINGHERE/ID
EDIT: (forgot to describe my issue),
When I try to load my page - domain.com/categoryname/PRODUCTID
The product page is called by the ID is not passed in.
Any ideas? I searched and could not find anything.
-Joe
I think the index is not zero-based. Try
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
Here is a good tutorial: Creating Rewrite Rules for the URL Rewrite Module

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>

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

HTTP Error 404.4 - Not Found The resource you are looking for does not have a handler associated with it

I've hosted a site in IIS but whenever I browse to the site I get 404.4. How can I solve this? I've referred several posts and they all say the issue is about staticfile but it is already mapped. What more can I do? Here is the attached picture of handler mappings in my iis 7.0
Any ideas?
EDIT:
I have this url rewriter set up:
<rules>
<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" />
</conditions>
<action type="Rewrite" url="https://abc.com/{R:1}" />
</rule>
When I disable this rule the http:// request is correctly handled. But when I enable it, I get this error.
Yet another update:
If I replace this:
<action type="Rewrite" url="https://abc.com/{R:1}" />
with
<action type="Redirect" url="https://abc.com/{R:1}" />
It all works out pretty well.
I was having the exact same issue. I installed Application Request Routing component and then set the Proxy enabled and selected the Use URL Rewrite to inspect incoming requests entered the redirect url alias and mine worked

Resources