IIS Redirects to multiple domains and preserves the request file path and names - redirect

I'm running IIS 8, Windows Server 2012.
My website has many sub-directories. An ideal url will look like:
oldsite.com/subdir/subdir/pages/default.aspx
I need to redirect (301) visitors to different domains and need to preserve the rest of the original url's file path (everything from the 2nd to the last slash "/").
I have a map of my redirects in an excel sheet, it looks like below:
oldsite.com/subdirA/subdirB/pages/default.aspx -> newsiteA.com/subdirC/subdirD/pages/default.aspx
So you see the whole thing has to change except the last part.
My web.config looks like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="RedirectsCollectionParshareSites">
<add key="/subdirA/subdirB/" value="/subdirC/subdirD/" />
<!--will have many of these add keys-->
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rule1 for RedirectsCollectionParshareSites">
<match url="^(.+)" />
<conditions>
<add input="{RedirectsCollectionParshareSites:{REQUEST_URI}}" pattern="^([^/]+)/([^/]+)(.*)" />
</conditions>
<action type="Redirect" url="http://newsiteA.com/{C:1}{C:3}" appendQueryString="true" />
</rule>
</rules>
My wish is to have many "add keys" - with the mapping from my excel sheet, and per new domain, I will have a new "rewriteMaps" section, with it's own "action".
My question is, is this the proper way of using this module? Any other suggestions?
Remember, I have about a thousand of these redirects to implement.
Any help would be appreciated.

I wasn't able to use the rewritemaps approach, but was able to do it by doing a rule per site:
<rule name="Rule:tes" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^(\/subdirA)(\/subdirB)(.*)" />
</conditions>
<action type="Redirect" url="http://newsite.com/subdirC/subdirD/{C:3}" />
</rule>
It's harder for admins to update, but it works.

Related

Custom domain and assets redirection using IIS Rewrite

I am trying to do a custom domain mapping where in i have to map custom.seconddomain.com that is the custom domain and my app is hosted at app.firstdomain.com.
So i have added a CNAME entry to domain provider for seconddomain.com. Then added an IIS entry for custom.seconddomain.com in my server that hosts app.firstdomain.com. I have added a Rewrite rule in this IIS entry for the app routes to be rewritten to app.firstdomain.com. Its all well and good till this point.
<rule name="Subdomain rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((http)?(s)?:\/\/)?custom.seconddomain.com\/(.*)" />
</conditions>
<action type="Rewrite" url="http://app.firstdomain.com/" appendQueryString="true" />
</rule>
But now since the assets and resources will only be available at app.firstdomain.com, its failing to load those assests on custom.seconddomain.com. So for that i added a rule on IIS Rewrite rule to redirect the assests request from custom.seconddomain.com to app.firstdomain.com, but not sure if its due to the precedence of the rules or something else. Both the rules can’t seem to operate together.
<rule name="Rule 1">
<match url="^((http)?(s)?:\/\/)?custom.seconddomain.com\/assets\/(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="https://app.firstdomain.com/assets/{R:4}" />
</rule>
I am new to IIS so was trying to achieve this through image hotlinking as well, to try and achieeve the desired outcome. Can you please let me know what am i missing here or doing wrong?

Best practice for maintaining lists of redirect urls with IIS 8?

Some website changes caused redirect lists with 400+ URLs. What is the best way to maintain these? Adding them one by one is not a good option. Adding them to the the web-config causes the file to grow to the point of the system being unable to read it.
Is there a way to maintain a separate file with redirects in IIS 8?
From my experience best way is to use rewrite maps:
Create rewriteMaps.config
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/test.aspx" value="/test2.aspx" />
<add key="/aboutus.aspx" value="/about" />
</rewriteMap>
</rewriteMaps>
Create rewriteRules.config
<rules>
<rule name="Rule for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
In web.config use configSource attribute
<rewrite>
<rewriteMaps configSource="rewriteMaps.config" />
<rules configSource="rewriteRules.config" />
</rewrite>
Benefits of this structure
Web.config small and clean
You can put complex redirects/rewrites with conditions in rewriteRules.config
You can put simple redirects (redirect pathA to pathB) directly into rewriteMaps.config as key-value pair

iis web.config redirect certain domain/path to a new domain/samepath

I need to use web.config to setup a redirect for a certain path/folder to a new domain using the same original path/folder.
Ex:
If www.domain1.com/path2match1/* is detected then redirect to:
www.newdomain.com/path2match1/* (same path as matched on domain1)
ex:
current: www.domain1.com/path2match1/someFileOrFolders/somefile.html
redirect to: www.newdomain.com/path2match1/someFileOrFolders/somefile.html
Any help would greatly be appreciated
thanks
I managed to find an answer for my question on my own! (Yay for me! haha)
Anyways,
Here's how to do what I was asking:
<rewrite>
<rules>
<rule name="NAME" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="(.*)/FOLDER/(.*)" />
</conditions>
<action type="Redirect" appendQueryString="false" url="http://DESTINATION-URL.TLD/FOLDERS/{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Caution! Adding multiple or tag set or placing this code in the wrong location will cause the site to generate internal server errors, making it unavailable for visitors until the additional tags are removed.
NAME can be anything, as long as it is unique
/FOLDER/ is the folder you are redirecting from
http://DESTINATION-URL.TLD/FOLDERS/ is where you are redirecting to.

301 Redirect one domain to another using web.config

I have multiple domains pointing to one hosting location.
I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.
For example:
www.example.com
This is my main domain. I want all other domains associated with my site to redirect to here.
If a user comes in on:
www.test.com or
www.test.com/anypage
etc.
Then I want the user to be redirected to the example version of that page.
How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.
I have attempted to do this redirect using the following code but it doesn't seem to work:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
</rule>
My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.
This is not really that umbraco related but I think what you want to do is this:
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.

IIS rewrite rule to redirect specific domain url to different url on same domain

I simply want an IIS 7.5 rewrite rule to redirect http://www.domain.com/url1 to http://www.domain.com/url2 (same domain). This can be achieved by:
<rule name="Redirect url" enabled="true" stopProcessing="true">
<match url="^url1" />
<action type="Redirect" url="http://www.domain.com/url2"
appendQueryString="false" redirectType="Permanent" />
</rule>
However, this website listens to several domains, thus above becomes a global rule for all domains. How do I make this specific to domain.com? Have tried changing match url and adding conditions but cannot get it to work. Thanks.
I got it to work this way:
<rule name="Redirect url1" stopProcessing="true">
<match url="^url1$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/url2"
appendQueryString="false" redirectType="Permanent" />
</rule>
Using the answer on this page, I was able to tweak a rule for myself. I also added query parameters. Wanted to post it here, in case it helps someone:
<!-- probably requires custom rewrite module, available through web platform installer -->
<rule name="Redirect oldsite.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
</conditions>
<action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&mode=form"
appendQueryString="false" redirectType="Permanent" />
</rule>
Some bits of explanation:
To clear up some confusion, this "url" is the part after the first slash after the domain, and not the whole url. I'm going to put in this so that it gets any URL.
<match url=".*" />
Now, we'll add a condition because I had more than one web site on this computer, so I want to make sure this rule is applied to only one of them. I also used the wildcard instead of "(www.)?" because the wildcard will catch any subdomain.
<conditions>
<add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
</conditions>
And the last note is for folks who want to put multiple query string params in. You'll need to escape the ampersand between them, because it won't work if you don't:
<action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&mode=form"
appendQueryString="false" redirectType="Permanent" />