简体   繁体   中英

Redirecting Non www to www. using URL Rewrite 2.0

I followed these instructions http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html

After some tweeking I can get it to work somewhat, but not completely how I would like. Here are my settings:

在此处输入图片说明

These settings work when i goto 'domain.com' by itself, it will redirect to 'www.domain.com'

However, When I goto an inner page on the domain, like 'domain.com/aboutus.html', it does not redirect to 'www.domain.com/aboutus.html', which is how I would like it to work.

I cannot use (.*) for a pattern, because there is a wildcard on the domain, which means if i type in anythingiwantto.domain.com it will redirect me to 'www.', which is not how I want it.

What I am looking for is the setting to not only redirect to 'www.domain.com' when it is ONLY 'domain.com' (no subdomains), but also to redirect any inner page in the same fashion.

FOR EXAMPLE:

anything.domain.com = does nothing

anything.domain.com/anything.html = does nothing

domain.com = redirect to www.domain.com

domain.com/anything.html = redirect to www.domain.com/anything.html

Also, I cannot use conditions to allow those subdomains individually, because there are too many subdomains to allow. I was thinking of adding a separate rule for inner pages, but I don't know how to write the expression for this.

    <rule name="Add WWW prefix to DOMAIN.COM">
      <match url="(.*)" ignoreCase="true"/>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^domain\.com"/>
      </conditions>
      <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent"/>
    </rule>

Same thing can be done to remove the prefix also. A blog post for the same at following URL:

http://karmic-development.blogspot.in/2013/10/add-prefix-www-automatically-in-url-in.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM