IIS redirect from subdomain to path - asp.net

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html

Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

Related

url rewrite / to default.aspx

We are experiencing some issues with our application where the default document (default.aspx) is not being picked up due the the appPool is set to Classic. It appears to be an issue documented here:
Site not redirecting to Default Document in Classic pipeline mode
to get around this we are trying to use the IIS URL Rewrite feature.
We want any request which comes to www.example.com/ to go to www.example.com/default.aspx
I used IIS to set it up on the root folder for the application, but i don't see it working, this is what it generated:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Default Documen Rewrite">
<match url="/" />
<conditions>
<add input="{QUERY_STRING}" pattern="/" />
</conditions>
<action type="Rewrite" url="/default.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT:
I just tried the following and it seems to be working, does anyone know if it's correct?
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="mysite/default.aspx" />
</rule>
</rules>
</rewrite>

Redirect single page to another domain

I need to redirect couple old webpages from my IIS to a new domain in a specific order.
For instance :
domain1/page1 to domain2/page1
domain1/page2 to domain2/page3
domain1/page3 to domain2/page5
...
Here's my webconfig <system.webserver> section:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect single page" patternSyntax="ExactMatch" stopprocessing="true">
<match url="domain1/page1"/>
<action type="redirect" url="domain2/page1" appendquerystring="false"/>
</rule>
</rules>
</rewrite>
<httpredirect enabled ="true" destination"Domain2" httpresponsestatus="permanent"/>
</system.webServer>
But the redirecting is not happening it still points to the old domain pages.
Accordingly the #Claus answer on the question: 301 Redirect one domain to another using web.config
You can use the following code to achieve this:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www.domain1.com$" />
</conditions>
<action type="Redirect" url="http://www.domain2.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<system.webServer>
This example will match all URLs unless the hostname part is exactly www.domain1.com - and redirect those to www.domain2.com/whatever.

301 redirects after domain name change

I run a small ASP.NET4 website on IIS. I recently changed the domain name from 'olddomain.com' to 'newdomain.com'. The change was made through Plesk. After making the change I added 'olddomain.com' as a domain alias in plesk so that traffic would still route through to the website on the old domain.
This is fine so far. The website resolves on both old and new URLs.
However, now I would like to set up 301 redirects everywhere so that any request for olddomain.com are forwarded to newdomain.com instead. None of the website pages have changed - it's just a simple change of domain name.
I've added this to the web.config file:
<rewrite>
<rules>
<rule name="Redirect all to different domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^olddomain.com$" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it doesn't seem to have any impact; when I load olddomain.com in the browser it just loads the website without redirecting to newdomain.com.
Can anyone suggest what I've done wrong here and how to get the 301 to work?
Thanks
This works:
<rule name="redirectDomain" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?olddomain\.com$" />
</conditions>
</rule>
You can set 301 permanent redirect from IIS also.
If http redirection is enabled on old server then you have to put new web config with the contents -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Let me know if it helps.

web.config redirection with query string to another domain

I have the current location on my website:
http://my-domain.com/ccy/app.html?from=GBP&to=EUR...
and I would like to use web.config to redirect all calls to /ccy folder to another domain containing the same query string, so it goes to.
http://my-other-domain.com/another-folder/app.html?from=GBP&to=EUR
How it is possible to achieve that using IIS7?
thanks in advance
You could use the Url Rewrite module in IIS 7. The following rule should work for you case:
<system.webServer>
<rewrite>
<rules>
<rule name="DynamicRewrite" stopProcessing="true">
<match url="ccy/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
</conditions>
<action type="Redirect" url="http://my-other-domain.com/another-folder/{R:1}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
You would want to use mod rewrite to rewrite any requests for ccy to another-folder.
Here's a link covering mod rewrite for IIS7.
http://www.micronovae.com/ModRewrite/ModRewrite.html

IIS URL Rewrite and Web.config

I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to example.com/page to example.com/page.html
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="/page" value="/page.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
A couple of problems arise:
I don't know where to even put the file. There is a User root directory, and an htdocs directory, I tried both, no joy.
I don't even know if the account can do rewrites, I am trying to find that out.
1) Your existing web.config: you have declared rewrite map .. but have not created any rules that will use it. RewriteMap on its' own does absolutely nothing.
2) Below is how you can do it (it does not utilise rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):
This rule will do SINGLE EXACT rewrite (internal redirect) /page to /page.html. URL in browser will remain unchanged.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^page$" />
<action type="Rewrite" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
This rule #2 will do the same as above, but will do 301 redirect (Permanent Redirect) where URL will change in browser.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Rule #3 will attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /page it will check if /page.html exists, and if it does then rewrite occurs):
<system.webServer>
<rewrite>
<rules>
<rule name="DynamicRewrite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/{R:1}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Just wanted to point out one thing missing in LazyOne's answer (I would have just commented under the answer but don't have enough rep)
In rule #2 for permanent redirect there is thing missing:
redirectType="Permanent"
So rule #2 should look like this:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Edit
For more information on how to use the URL Rewrite Module see this excellent documentation: URL Rewrite Module Configuration Reference
In response to #kneidels question from the comments; To match the url: topic.php?id=39 something like the following could be used:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^topic.php$" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="(?:id)=(\d{2})" />
</conditions>
<action type="Redirect" url="/newpage/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This will match topic.php?id=ab where a is any number between 0-9 and b is also any number between 0-9.
It will then redirect to /newpage/xy where xy comes from the original url.
I have not tested this but it should work.
Just tried this rule, and it worked with GoDaddy hosting since they've already have the Microsoft URL Rewriting module installed for every IIS 7 account.
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^enquiry$" />
<action type="Rewrite" url="/Enquiry.aspx" />
</rule>
</rules>
</rewrite>

Resources