IIS Rewrite map - can both key and value be URLs?

Blake Duffey 26 Reputation points
2022-08-29T21:51:29.28+00:00

Have good success with using rewrite maps using the samples:

<rewriteMaps>  
    <rewriteMap name="StaticRedirects">  
        <add key="/old-article.aspx?id=1" value="/article.aspx?id=1" />  
        <add key="/posts/default.aspx?id=1" value="/article.aspx?id=1" />  
        <add key="/old-title.html" value="/article.aspx?id=1" />  
    </rewriteMap>  
</rewriteMaps>  

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

My question is - I want both the key and the value to be separate URLs. I have IIS listening on *, a wildcard cert accepting *.foo.com - and I need to redirect according to these kinds of rules

<add key="s2.foo.com" value="s1.foo.com" />  
<add key="s3.foo.com" value="s1.foo.com/somespecialpage" />  

I've seen some limited examples where the key is a URL, and others where the value is a URL - but I'm unable to find one where both is a FQDN.

While I realize I could build a series of standard rules, I feel the map would be more elegant.

Thx

Internet Information Services
No comments
{count} votes

Accepted answer
  1. Yurong Dai-MSFT 1,386 Reputation points Microsoft Vendor
    2022-08-30T09:05:06.533+00:00

    Hi @Blake Duffey

    From your description, my understanding is that you want to redirect s2.foo.com to s1.foo.com and redirect s3.foo.com to s2.foo.com/somespecialpage by rewrite map, please point out my mistakes if I understand incorrectly.

    In rewrite map, it is not possible to have {HTTP_HOST} in both key and value. If you want to implement multiple redirects, it is recommended to create blank rules separately. Even though this would be repetitive, this is the least error prone method.


    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

1 additional answer

Sort by: Most helpful
  1. Blake Duffey 26 Reputation points
    2022-08-30T14:25:50.83+00:00

    Thanks for your reply - I've actually edited my initial question but I don't think it makes a difference to the final answer. The IIS site is intended to be a 'catch all' for a variety of hostnames on the same domain - so the map might look like this

    <add key="s2.foo.com" value="s1.foo.com" />  
    <add key="s3.foo.com" value="s1.foo.com/somespecialpage" />  
    <add key="s4.foo.com" value="s1.foo.com/someotherpage" />  
    <add key="s5.foo.com" value="s1.foo.com" />  
    

    You get the idea. If I can't leverage {HTTP_HOST} in both key and value then I'm stuck creating rules for each. Repetitive indeed.

    Thanks very much for your feedback.

    1 person found this answer helpful.
    No comments