IIS URL Rewrite Map is not work.

Eskioglu, Salih 20 Reputation points
2024-08-27T11:16:59.32+00:00

Hello

When I write URL Rewrite rules, they are works. But I want to use URL Rewrite Map. Bacause i have got a lot of page for rewrite and to write in rule is hard.

But when I use the URL Rewrite Map, It was not work. It cannot rewrite page on "value". When I used defaultvalue, It was work. It is mean that my rule is working and my rule can reach to map part on web.config file. But It is not work. I tried https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/rule-with-rewrite-map-rule-template sample code. But when I run it, It says 404 pahe not found. It says "Requested Url: http://localhost:80/article1" and "Physical path: "c:\inetpub\wwwroot\article1".. It looks like the map cannot open artcile.aspx file. I think that the physical path must be "c:\inetpub\wwwroot*article.aspx*" but the system try open my short name instead of my physical page.

Internet Information Services
{count} votes

Accepted answer
  1. Lex Li (Microsoft) 5,582 Reputation points Microsoft Employee
    2024-08-27T20:31:11.4233333+00:00

    Rewrite map should store the complete URL strings, not part of them or patterns.

    Thus, you need to use,

    <configuration>
      <system.webServer>
        <rewrite>
          <rewriteMaps>
            <rewriteMap name="StaticRewrites">
              <add key="/url_rw/mainpage" value="/url_rw/index.aspx" />
            </rewriteMap>
          </rewriteMaps>
          <rules>  
            <rule name="Rewrite Rule 1 for StaticRewrites" stopProcessing="true">  
              <match url=".*" />  
              <conditions>  
                <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />  
              </conditions>  
              <action type="Rewrite" url="{C:1}" appendQueryString="False"/>  
            </rule>  
          </rules>  
        </rewrite>
      </system.webServer>
    </configuration>
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Eskioglu, Salih 20 Reputation points
    2024-08-27T17:43:29.0466667+00:00

    My web.config file as below:

    <configuration>
    <system.webServer>
    <rewrite>
    <rewriteMaps>
    <rewriteMap name="StaticRewrites">
    <add key="/mainpage" value="/index.aspx" />
    </rewriteMap>
    </rewriteMaps>

    <rules>  
        <rule name="Rewrite Rule 1 for StaticRewrites" stopProcessing="true">  
            <match url=".*" />  
            <conditions>  
                <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />  
            </conditions>  
            <action type="Rewrite" url="{C:1}" appendQueryString="False"/>  
        </rule>  
    </rules>  
    

    </rewrite>
    </system.webServer>
    </configuration>

    So this code came from Microsoft web page. I just changed key and value parameters. So my web.config file stored on url_rw folder.

    But when I open localhost/url_rw/mainpage, the systen says "404 not found" as below screenshot. I yellow marked requested url and physical path. It think that I must be index.aspx on physical path file.

    url_rw_error.png

    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.