URL Rewrite/Redirect not following its own rules

MEccle$ 21 Reputation points
2022-09-23T21:21:14.4+00:00

I am trying to set up Redirects to various pages on a website. For example, I want something like this:

www.example.com/about -> https://www.example2.com/about-us.

This works fine. However, I also want something like this:

www.example.com/about/contact -> https://www.example2.com/contact/

I have created the rules, and the way I understand it is that the contact one needs to be written first. I have done that, but the contact rule instead redirects to https://www.example2.com/about-us/.

Here are the rules as I have written them:

<rule name="Contact Page Redirect" enabled="true" stopProcessing="true">
<match url="^about/contact/" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="https://www.example2.com/contact/" appendQueryString="false" redirectType="Found" />
</rule>
<rule name="About Page Redirect" enabled="true" stopProcessing="true">
<match url="^about" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Redirect" url="https://www.example2.com/about-us" appendQueryString="false" redirectType="Found" />
</rule>

I even tried adding <add input="{REQUEST_URI}" pattern="^about/contact" negate="true" /> inside of the conditions on the About Page Redirect rule.

What am I missing? No matter what I try the contact redirect never goes to the URL specified.

Internet Information Services
0 comments No comments
{count} votes

Accepted answer
  1. XuDong Peng-MSFT 10,096 Reputation points Microsoft Vendor
    2022-09-24T05:10:11.01+00:00

    Hi @MEccle$ ,

    Yes you are right, the order of configuration affects the order in which this function is matched.

    Based on your description, I did some test and reproduced your issue on localhost. The mainly problem is the match rule regular expression <match url="^about/contact/" /> .
    244406-image.png

    You need to pay attention to every symbol of it, and in your case you seem to be adding an extra / sign at the end. This will result in no match for www.example.com/about/contact, you must type the full address correctly: www.example.com/about/contact/ to match it.

    Therefore, you need to modify this configuration to something like this:

       <rule name="Contact Page Redirect" enabled="true" stopProcessing="true">  
          <match url="^about/contact" />  
            .............  
            .............  
       </rule>  
    

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. 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 related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. MEccle$ 21 Reputation points
    2022-09-24T07:56:38.147+00:00

    Hi @XuDong Peng-MSFT ,

    Can special characters also be an issue?

    I am doing something similar with another URL:

    I am trying to take www.example.com/press-releases/really-long-title-here-with-a-number-in-parentheses-(1)/ and redirect to www.example2.com.

    When I test the pattern in the URL Rewrite tool, I made sure it was identical, and still I get no matching pattern.

    I ask because in relation to the first one, the underlying URL is about/#contact.

    I am using the Rewrite tool, but here is what both look like in the rules now:

    <rule name="Contact Page Redirect" enabled="true" stopProcessing="true">
    <match url="^about/#contact" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Redirect" url="https://www.example2.com/contact" appendQueryString="false" redirectType="Found" />
    </rule>
    <rule name="About Page Redirect" enabled="true" stopProcessing="true">
    <match url="^about" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    </conditions>
    <action type="Redirect" url="https://www.exmaple2.com/about-us" appendQueryString="false" redirectType="Found" />
    </rule>

    What is happening now tiht his one is the COntact Page Redirect goes to www.example2.com/about-us/#contact instead of www.example2.com/contact

    Likewise, here is the rule for the other failing Redirect I have with special characters:

    <rule name="Press Release Stuff Redirect" stopProcessing="true">
    <match url="^press-releases/really-long-title-here-with-a-number-in-parentheses-and-at-this-point-i-just-need-something-that-is-one-hundred-and-fifty-seven-characters-long-almost-theres-(1)/" ignoreCase="true" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Redirect" url="https://www.example2.com" redirectType="Found" />
    </rule>

    I have checked every other redirect that wasn't working, and now it is, because I can match the patterns. It is just these two that don't seem to want to match, even if I type them in correctly when testing the pattern.


  2. MEccle$ 21 Reputation points
    2022-09-26T04:40:24.987+00:00

    Thank you. I'll redo the URLs to not have the special characters.

    0 comments No comments