0
votes

Possible duplicate of https://stackguides.com/questions/26840890/i-need-to-change-domain-with-iis-7-5-url-rewrite (unanswered yet)

I have the following URLs

www.myolddomain.com/somefolder-a
www.myolddomain.com/somefolder-b
www.myolddomain.com/somefolder-c
...

I want one rule to redirect every request incoming containing myolddomain.com/somefolder to www.mynewdomain.com/somefolder with above suffix such as -a.

My rule so far looks like this:

<rule name="Redirect from old domain" enabled="true" stopProcessing="true">
  <match url="(.*)myolddomain.com/somefolder(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
  </conditions>
  <action type="Redirect" url="https://www.mynewdomain.com/somefolder{R:2}" />
</rule>

This rule has no effect whatsoever.

Did I make a mistake here already? Or could requests be redirected at some other place before IIS?

According to the firewall admin no redirects take place at the firewall. Could it be anything else?

1

1 Answers

0
votes

Through further trial and error I found a solution:

<rule name="Redirect from old domain" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="myolddomain.com" />
        <add input="{HTTP_URL}" pattern="/somefolder(.*)" />
    </conditions>
    <action type="Redirect" url="https://www.mynewdomain.com/somefolder{C:1}" />
</rule>

For curiousity, how is the match on the rule level supposed to work? No matter what I put there, as soon as it is anything else than .* the rule is ignored.

Hint to others: don't fall into the trap of thinking that server variables like "HTTP_URL" actually contain the URL - they do NOT. Best way to figure out what is really in which variable is a sample page like this (copied from the web - I apologize for having lost the link):

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server Variables</title>
</head>
<body>
<table>
<tr>
<th>Server Variable</th>
<th>Value</th>
</tr>
<tr>
<td>HTTP_URL: </td>
<td><%= Request.ServerVariables["HTTP_URL"] %></td>
</tr>
<tr>
<td>Replace above server variable to retrieve different variables</td>
</tr>
</table>
</body>
</html>