Search code examples
iismod-rewriteurl-rewriting

IIS Rewritemap to ignore all querystrings


We have made a new site for a customer. He had an old site, and we importerd the old sitemap to a rewritemap file for IIS.

The problem is, they seem to use a lot of querystrings in the old URLS. So /projects/project1?page=2 for example. The new site doesn't use these querystrings.

Is there a way to just ignore all querystrings for the redirectmap? I want to redirect if the source-without-querystring exists in the rewritemap. So if the input is /projects/project1?page=2, I want it to find /projects/project1/ in my rewritemap.

The rules are as follows:

<rewrite>
  <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
  <rules>
    <rule name="Redirect rule1 for Redirects">
      <match url="(.*[^/])" />
      <conditions>
        <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Solution

  • Based on your description, what I understand is that you don't want to include the query string in the match, please correct me if I understand wrong.

    The {Redirects:{REQUEST_URI}} condition in your rule checks whether there is a key in this rewrite map matching the REQUEST_URI server variable. Note that this variable contains the query string:

    {REQUEST_URI} = /projects/project1?page=2
    

    If you don't want to include the query string in the match, use the {PATH_INFO} server variable instead of {REQUEST_URI}.

    {PATH_INFO} = /projects/project1
    

    <rewrite>
          <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
          <rules>
            <rule name="Redirect rule1 for Redirects">
              <match url="(.*[^/])" />
              <conditions>
                <add input="{Redirects:{PATH_INFO}}" pattern="(.+)" />
              </conditions>
              <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>