Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

What's the safest, and most effective way of redirecting traffic from the www.example.com to example.com or vice versa with IIS?

share|improve this question

migrated from stackoverflow.com Sep 16 '11 at 22:48

This question came from our site for professional and enthusiast programmers.

    
because you can't use python to solve it? –  Daniel Sep 16 '11 at 18:19
    
If it's a programming task, you can probably use any language to solve it, but as @cdeszaq points out, it may just be a matter of changing some entries in a table. In any case, I'm sure the people at webmasters will know exactly what to do. –  Tom Zych Sep 16 '11 at 18:23
    
@Daniel - No, because it is more related to servers and server configuration than it is to programming. IIS is a server. –  cdeszaq Sep 16 '11 at 18:23

3 Answers 3

up vote 0 down vote accepted

The best way is to do it using DNS entries. That way, your IIS server isn't even involved.

share|improve this answer
    
Is there a way to edit the web.config file to do this? –  Daniel Sep 16 '11 at 18:21
    
@Daniel - No, DNS is a separate service that maps domain names (and their parts) to IP addresses and physical services that the underlying network routing infrastructure can understand. –  cdeszaq Sep 16 '11 at 18:24
    
@Daniel - That being said, you can configure IIS to do the redirect, but if your DNS entries are not properly set up, it won't work correctly. And like I said, it is better to do the redirect via DNS anyways since it is less network traffic and fewer server requests meaning your pages will load faster. –  cdeszaq Sep 16 '11 at 18:26
    
DNS entries are not sufficient to solve the problem. The DNS entries must point to a server AND that server must be configured to redirect. –  Stephen Ostermiller May 24 at 18:23
    
This answer is incorrect, DNS cannot issue a 301 or 302. It can CNAME to the same place (IP, another CNAME, etc.), but that in no way redirects the browser - an HTTP server must issue a redirect response for that to happen. A ton of us wish this was possible in DNS alone, but it isn't. –  Nick Craver May 25 at 23:16

Add an A NAME record of * and then your ip address of your hosted server to your DNS settings of your domain name.

share|improve this answer

For SEO purposes you have to make a decision if you want to go with either example.com (without www.) or with www.example.com and stick with it. Do not use both.

You should set up your DNS to let both point to your web server and bind your website to both host names so that both work in case your visitor types either in his browser.

Then, using the URLRewrite module, you can 301 redirect all traffic to the domain name with or without www., whatever you prefer to use for your site.

E.g. if you want to use www.example.com then you can use the following redirect rule:

<rule name="Force to use www.example.com" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
  </conditions>
  <action type="Redirect" url="http://www.example.com{QUERY_STRING}" appendQueryString="false" redirectType="Permanent" />
</rule>

If you want to use just example.com then use this:

<rule name="Force to use domain.com" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_HOST}" negate="true" pattern="^example\.com$" />
  </conditions>
  <action type="Redirect" url="http://example.com{QUERY_STRING}" appendQueryString="false" redirectType="Permanent" />
</rule>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.