0

I want to all www domain to naked via IIS Rewrite engine. There are many domains pointing to same application.

Here is my rule :

^(www.)(.*)$

Action Type : Redirect Redirect URL : {R:2} Redirect Type : Permanent

When i test pattern for www.xxx.com

R:0 => www.stackoverflow.com R:1=> www. R:2 => www.stackoverflow.com

and that is fine. What is wrong? And also should I include "http://" ?

gandil
  • 5,398
  • 5
  • 24
  • 46
  • I added a back reference to the condition. Is that what you were looking for? Basically take off the parens around the first one (or deference it or count it) and do something like this: ^www\.(.+)$ (for the HTTP_HOST) and redirect to http://{C:1}//{R:0} – craniumonempty Dec 12 '11 at 14:01
  • if you just do the ^www\.(.+)$ that will match the entire thing with the "www." if you want specific you can do this: ^www\.(.+)\.(com|net|org)$ for "com" or "net" or "org" – craniumonempty Dec 12 '11 at 14:08
  • Oh, also add redirectType="Permanent" to the action tag to make it permanent.. ooh, it should be ^www\.(.+\.(com|net|org))$ or you'll have to call 2 back references for that second part – craniumonempty Dec 12 '11 at 14:11

1 Answers1

2

Something like ^(www\.)(.+)$ should work when matching the http_host, but it might be better to specify the domain. From what I know of IIS (not much) and what it says on the net, something like:

<rewrite>
    <rules>
        <rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.domain\.com$" />
            </conditions>
            <action type="Redirect" url="http://domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

Oh, you said for any domain. If you want to make sure it ends in .com it should be something like

^(www\.)(.+)(\.com)$ against HTTP_HOST

.. oh, if you do that you need to back reference, so try something like this:

<rewrite>
    <rules>
        <rule name="Redirect www.domain.com to domain.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
            </conditions>
            <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

The c:1 is a back reference

craniumonempty
  • 3,525
  • 2
  • 20
  • 18
  • the problem is our domains are not specific. we generate content from database based on url. so I want to redirect all www domains to naked. There are about 100 domains pointing to that sites. This can be much more. So I cant specify domain. any suggestions? – gandil Dec 12 '11 at 13:53
  • @daemon udated last to include rest of host name and make it permanent. If you want it more dynamic, read up on regex. – craniumonempty Dec 12 '11 at 14:18