0
votes

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://" ?

1
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
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
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 partcraniumonempty

1 Answers

2
votes

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