Question

I have seen a handful of similar questions on handling removing the www from a URL, but I was needing to remove it only when a certain directory exists. The real issue is that I run the "test" in URL Rewrite within IIS and it works fine, but for some reason does not respond when I type in the URL. I have tried switching to when "not matches" and it redirects, so I am a bit baffled what I am doing wrong to get this to work.

An example URL would be http://www3.test.com/feed/testing.xml If the directory "feed" exists in the URL then remove the www3. I thought this would be fairly simple.

<rule name="fix feeds" enabled="true" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="(www3\.)(.*)(feed/.*)" />
    </conditions>
    <action type="Redirect" url="http://{C:2}{C:3}" appendQueryString="false" redirectType="Permanent" />

Another issue I am finding is that if you set it to permanently redirect 301, then once you get something to fire (and may not work as expected) the browser caches the 301 redirect and makes a mess of your testing environment.

Was it helpful?

Solution

You can't match feed in HTTP_HOST instead you need to use Match URL.

You have to match /feed/testing.xml in match URL which will yield here R:0 Match your patter if it has www3 and take C:2 which will give you URL without www3.

<rule name="fix feeds" enabled="true" stopProcessing="true">
  <match url="^feed/.*" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="(www3\.)(.*)" />
    </conditions>
    <action type="Redirect" url="http://{C:2}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top