0

I'm trying to do a simple redirect from the www subdomain to the root domain. After trying many variations, I'm stuck and hoping for help. Feeling sure it's something stupid, but can't see it. Here's the rule I'm using.

<rewrite>
  <rules>
    <rule name="Redirect to root" stopProcessing="true">
      <match url=".*"  />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www.stitchamerica.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="http://stitchamerica.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Input greatly appreciated.

1 Answers1

0

This works for me:

    <rewrite>
      <rules>
        <rule name="Remove WWW" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="http://YOURSITE.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
illinoistim
  • 456
  • 10
  • 27
  • FYI, If you have URL Rewrite installed, you can access it through IIS. Double click and add rules is the easiest way to write these rules. – illinoistim Mar 28 '14 at 18:40
  • hanks for the answer but your suggestion is not working either. I copied your code and substituted my domain name but No Bueno. Is there something I'm missing? Some setting in IIS7 that is overriding? – maverickbluer1 Mar 28 '14 at 18:49
  • This is an MVC application. Could that be causing the issue? – maverickbluer1 Mar 28 '14 at 18:53
  • Interesting, I can use that to redirect to www., but not to remove it. I will need to research this some more. – illinoistim Mar 28 '14 at 19:19
  • Okay, I think that should work. I had to change the match url and condition pattern. – illinoistim Mar 28 '14 at 19:27
  • Wow, thanks again but that's not working either. I installed the URL Rewrite module and set up a rule that also did not work. Screenshot attached below. Was hoping to avoid by setup Trace Failed request on web.config to get more information. Were you able to verify your suggestion above? – maverickbluer1 Mar 28 '14 at 19:52
  • Based on the rule testing in the URL Rewrite Module the URL noted would have resulted in double slashes so also tried: http://yourdomain{r:1} to no avail. – maverickbluer1 Mar 28 '14 at 19:57
  • The pattern is wrong. You should be able to copy what I have into your config file and only replace the YOURSITE part. – illinoistim Mar 28 '14 at 20:02
  • My bad! I was editing a web.config file on my desktop but then uploading the file in the project (which wasn't changed). Thanks again for your help. It works just fine when I got the right code uploaded EXCEPT it always redirects to the root and leaves off any of the request. Looks like there should be another variable on the end. – maverickbluer1 Mar 28 '14 at 22:16
  • So the URL needs to have the variable {R:0} concatenated onto it. – maverickbluer1 Mar 28 '14 at 22:21
  • Ah, yes, I didn't include {R:0}, because my site didn't need it. I have added it to the answer to those who come across this. – illinoistim Mar 31 '14 at 16:21