IIS Web.config: redirect non-www to www

Hi All,

Could someone help me with the web.config code i need to ensure all websites are prefixed with www.

Many thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom CieslikIT EngineerCommented:
To redirect requests without the www to www on your website, add the following rules to your web.config.
Make sure your.domain.name below is changed to your domain name.
NOTE: If you already have a web.config with rules configured, add the rule below, otherwise a full web.config example is below it.


        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
            <add input="{HTTP_HOST}" pattern="^your.domain.name$" />
          <action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />

Full Web.Config with Rule

        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
            <add input="{HTTP_HOST}" pattern="^your.domain.name$" />
          <action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />
detox1978Author Commented:
thanks.   I have multiple websites.  is there a catch all?
Tom CieslikIT EngineerCommented:
What do you mean multiple websites ?
Are they on same domain ?

Redirection is working from non www domain and forwarding request to www.doman.com
If you have few websites with different name like

then you need to create more rules but if user will put http://domain.com in browser then only first rule will work.
carlos sotoIT AdministratorCommented:
you could use the redirection function in IIS. See the link below

Dan McFaddenSystems EngineerCommented:
1. What version of Windows Server are you using?

Assuming you are using Server 2008 or newer, you could do this in IIS Manager, in the following way:

1. Make sure that the URL Rewrite feature is installed on the server
2. Open IIS Manager
3. Select the Server object in the GUI
4. Go into the URL Rewrite feature
5. Click "Add Rule(s)" in the right-hand action panel
6. Select "Blank rule" and click OK
7. Enter a rule Name.  For example:  "Global add WWW to domain"
8. Leave the defaults of "Matches the Pattern and Regular Expressions in the "Match URL section.
9. In the Pattern field, enter the following:  (.*)
10. Expand the "Conditions" section and click the "ADD" button to add a condition
11. In the "Condition input" field, enter:  {HTTP_HOST}
12. In the "Check if input string" drop down, select:  Does not match the Pattern
13. In the "Pattern" field, enter:  ^www\.([.a-zA-Z0-9]+)$
14. Click OK
15. Expand the "Action" section
16. In the "Action Properties, Rewrite URL" field, enter the following:  http://www.{HTTP_HOST}/{R:0}
17. In the right-hand Actions panel, click Apply and then Back to Rules

Reference link:  https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-global-and-distributed-rewrite-rules

The Server scope configuration is contained in a file called applicationHost.config, which is located in the following directory:  C:\Windows\System32\inetsrv\config.  I recommend using IIS Manager to modify IIS at this scope.  Incorrectly editing the file directly can lead to IIS not functioning properly.

As a side note:  an important thing to remember when managing an IIS Server.  In IIS, there are 2 scopes (levels) for configuration:

1. Server scope (global) = configurable options are visible when the Server object is selected in the navigation panel.
2. Site scope (local) = configurable options are visible when the Site object is selected in the navigation panel.

Not all options are available at the Server scope, but everything that is available at the server scope, can be overridden at the local scope.  So you can deploy inheritable configuration that is setup at the server.  When necessary, that inherited configuration can be overridden down at the Site scope.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.