3

I've found this code to add the www to urls without it using url rewrite.

<rewrite>
    <rules>
        <clear />
        <rule name="WWW Rewrite" enabled="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
            </conditions>
            <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

But it seems to not work of the url has a '-' in it, such as scotts-cleaners.com.

That returns www.www.scotts-cleaners.com.

Any ideas?

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
Bth
  • 423
  • 1
  • 4
  • 11

2 Answers2

1

Simply add - to the pattern:

<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9-]+)$" />

Since hyphen and alphanumeric constitute the only allowed characters in a domain name, your pattern should now work for all URLs.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0
pattern="^www\.([.a-zA-Z0-9-]+)$"

apparently hyphens don't need escaping in regex ^^

maddrag0n
  • 459
  • 4
  • 13
  • You don't escape the hyphen within a bracket expression (range). Use `"^www\.([.a-zA-Z0-9-]+)$" instead. – ghoti Feb 06 '12 at 13:00