IIS 301 redirect

Ali Yılmaz 81 Reputation points
2023-04-26T11:19:31.61+00:00

Hi, My goal is to do a 301 redirect. But the url on the site I just moved is different. Naturally, I write the urls one by one. So what should I do in this situation? I want to do it with rewrite config. But how do I get the url of the new site. Web.Config


 <rule name="Redirect rule1 for Redirects">
        		<match url=".*" />
       			 <conditions>
         		 <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
        		</conditions>
       		      <action type="Redirect" url="{C:1}" appendQueryString="false" />
      		</rule>

Rewritemaps.config


<rewriteMaps>

  <rewriteMap name="Redirects">

    <add key="/oldsite" value="https://www.newsite.com/de-DE/p/newproduct" />  

  </rewriteMap>
</rewriteMaps>
Windows development | Internet Information Services
{count} votes

1 answer

Sort by: Most helpful
  1. Yurong Dai-MSFT 2,846 Reputation points Microsoft External Staff
    2023-04-27T06:52:55.8166667+00:00

    Hi @Ali Yılmaz,

    For your needs, I think it would be feasible to create a rewrite map that defines a static mapping between the source URL and the redirect URL.

    <rewriteMaps>
        <rewriteMap name="Redirects">
            <add key="/oldsite" value="/de-DE/p/newproduct" />
        </rewriteMap>
    </rewriteMaps>
    

    Here you need to add a rewrite map according to your actual situation. For which old URLs you need to redirect to new URLs, add the corresponding keys and values.

    Notice that there is no obvious common pattern in the keys and their relation to values. This means that it is not possible to use regular expressions or wildcards to define URL transformation logic. Further, this means that if we had not used rewrite maps we would have had to create three rewrite rules. With rewrite maps we can create only one rewrite rule.

    The next step is to create a rule that uses "Redirects" rewrite map:

    <rules>
        <rule name="Redirect Rule" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
            </conditions>
            <action type="Redirect" url="https://www.newsite.com{C:1}" appendQueryString="False" redirectType="Permanent" />
        </rule>
    </rules>
    

    <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)">

    To perform this check the value of the server variable REQUEST_URI is passed as a parameter to the rewrite map. If rewrite map contains an entry with key, that is the same as REQUEST_URI, then the value corresponding to that key will be returned. The regular expression pattern will match only non-empty strings, so if empty string was returned from the map then the condition will evaluate to false, hence no rewriting will be performed. If non-empty string was returned then that string will be captured in a back-reference, because of the parenthesis used in the pattern.

    <action type="Redirect" url="http://localhost{C:1}" appendQueryString="False" redirectType="Permanent" />

    This element specifies that URL rewrite module needs to redirect web client to a new URL that is constructed by concatenating the new domain name and the redirection URL path returned by "Redirects" map.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

    Best regards,

    Yurong Dai

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.