Scott Forsyth's Blog

Postings on IIS, ASP.NET, SQL Server, Webfarms and general system admin.

.

  • Scott Forsyth

Hosting Needs

Training and Dev Labs

IIS URL Rewrite – rewriting non-www to www

If you’re using IIS 7.0 (or 7.5), URL Rewrite is a valuable tool, well worth installing and using.

One common use of URL Rewrite is redirecting http://domain.com to http://www.domain.com.  Many people are doing this for search engine optimization (SEO) so that search engines only see the one site, rather than two sites.  The goal is to set a permanent 301 redirect.

You can download URL Rewrite from http://www.iis.net/expand/URLRewrite.  For this walkthrough and screenshots I’ll use URL Rewrite 2.0 RC1, but everything that I’ll cover also works for version 1.0 and 1.1.

URL Rewrite works at the global level, or site level (or application level for that matter).  Where you apply it is really up to how you manage your server.  Either will work for a domain name redirect like this.

You can choose to create the rules using IIS Manager, or using a text editor and updating web.config directly.  I’ll show both, starting with IIS Manager.

Let’s get started.  First, open IIS Manager and double-click on the “URL Rewrite” icon.

image

Next, click on “Add Rules…” from the Actions pane.

Here you’ll have a choice from a few wizard options, and with URL Rewrite 2.0 you can also create outbound rules.  Create a Blank rule (inbound rules).

image

Give your rule a good friendly “Name”.  I’ll call mine “Redirect domain.com to www”.

In the “Using” dropdown box you can choose between Regular Expressions and Wildcards.  Use wildcards if you aren’t familiar with regular expressions since they are much more intuitive.  However, if you later need to create more complex rules, regex may be necessary.

For this demo select WildcardsHowever, I’ll include instructions for those wanting to use regular expressions.

Enter * for the “Pattern”.  That means anything qualifies.  We’ll use a condition later instead of matching to the URL.  (for Regular Expressions, use .*).

Now expand the “Conditions” section and click “Add”.  In the “Add Condition” dialogue enter the following:

Condition input: {HTTP_HOST}
Check if input string: Matches the Pattern
Pattern: domain.com
(for regex, enter ^domain.com$)
Ignore case: checked

image

Click OK.

Finally, it’s time to set the Action.

In the Action section make sure that the “Action Type” is set to Redirect

For the “Action Properties”, enter http://www.domain.com/{R:0}.  The {R:0} retains the existing URL so if someone typed something like http://domain.com/aboutus it would retain the aboutus as it adds the www.

Note (added later): If you want this to be more generic to account for multiple domain names and to retain the original domain name in the redirect, check out this blog post.

Be sure that the “Append query string” remains checked so that the querystring part is also retained.

Also, be sure that the “Redirect Type” is set to Permanent (301), which is what the search engines like.  This tells the search engines to do a permanent redirect, use the new location and ignore the previous location.

image

Finally, Apply the rule and test!

Using a Text Editor

You can also create this rule manually by adding the following to your site’s web.config (or applicationHost.config if you set this at the server level).

In the <system.webServer> section of your web.config, add the following:

Wildcards

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="domain.com" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

 

Save and you should be set.

Or, if you prefer Regular Expressions, use this instead:

Regular Expressions

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^domain.com$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

This is just the start to great SEO, but it’s a common step and one that I hope you find helpful.

See Part II on how to redirect multiple domain names to a single domain name: http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx

Also see this blog post on how to do a generic redirect while retaining the original domain name.

Posted: Nov 27 2009, 05:55 PM by OWScott | with 57 comment(s)
Filed under: , ,

Comments

IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog | Webmasters feeds said:

Pingback from  IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth&#39;s Blog | Webmasters feeds

# November 28, 2009 5:09 AM

Jason Haley said:

Interesting Finds: November 28, 2009

# November 28, 2009 8:27 AM

IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog Search Engine Marketing said:

Pingback from  IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth&#39;s Blog Search Engine Marketing

# November 28, 2009 10:35 AM

Twitter Trackbacks for IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog         [asp.net]        on Topsy.com

# November 28, 2009 12:36 PM

r4 dsi said:

Wow it good to know that using IIS Manager there are so many new options available. I normally like my net history categorized. Yes its just a step towards great SEO.

# November 30, 2009 5:54 AM

rubens said:

If there any generic way to do that if we have multiple domains with multiple extensions?

example:

mydomain.com to www.mydomain.com (already done)

mydomain.net to www.mydomain.com

www.mydomain.net to www.mydomain.com

etccc...

and maybe also

www.mydomain2.net to www.mydomain.com

etccc...

Thanks.

# November 30, 2009 3:19 PM

OWScott said:

That's where regular expressions and multiple conditions come in.

To get you started, if you switch to regular expressions and use the example in my blog, then for the Condition Pattern, rather than ^domain.com$, switch it to ^domain.(com|net)$.  That will catch both .com and .net.

If you are redirecting them all to the same domain it's easy.  You don't need to update the Redirect URL.

If you do want to retain the .com or .net you can use {C:1} instead of com the redirect URL, but that's likely not what you're worried about.

To get mydomain2.net, just switch the Logical grouping dropdown in the conditions to "Match any" and add as many conditions as you want.  As long as any of them catch, it will redirect.

Obviously make sure that you don't put a condition that is the same as the redirect URL, otherwise you'll get a loop.

Note, you can do this with wildcards instead of regular expressions.  Just create a new condition for each of .com and .net and mydomain2.net.

# November 30, 2009 3:48 PM

rubens said:

Thanks a lot for the reply. I'm testing things right now and succeeded making it work with 8 rules.

I use only the web.config code since it's quicker.

If I want to make a rule that redirects any domain (www or non www; .net.org) to only one how would you do it?

Thanks,

# November 30, 2009 4:50 PM

OWScott said:

Hi Rubens.  I figured it would be worth answering that with a blog post where I can include screenshots: weblogs.asp.net/.../iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx.  Hopefully that helps.

# November 30, 2009 5:43 PM

IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth's Blog said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth&#39;s Blog

# November 30, 2009 5:47 PM

rubens said:

I appreciate that very much !!!!

Thanks.

# November 30, 2009 6:12 PM

Scott Forsyth's Blog said:

Consider this a 2nd part to IIS URL Rewrite – rewriting non-www to www .&#160; Reader Rubens asked about

# November 30, 2009 6:27 PM

IIS URL Rewrite ??? Redirect multiple domain names to one | I love .NET! said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one | I love .NET!

# November 30, 2009 7:44 PM

nidhi tiwari said:

if we create web.config file on client's site becoz it was not there thn how can we use it for url redirection from www url to non www url and vise versa

# December 10, 2009 7:32 AM

Scott Forsyth said:

As long as your server (or host) supports URL Rewrite, then you can add the config directly to web.config in the <system.webServer> section and it should work for you.  It needs to be installed on the server for it to take though.

# December 10, 2009 9:21 AM

David Moore said:

I am a web host and have multiple sites on the server. I am using host headers to manage most of the websites. I have been successful in setting up this rule for the default website where upstateweb.com will redirect to www.upstateweb.com. The problem comes when I set up the same rule for any of the other domains. The domain name does not redirect and pulls up upstateweb.com. In otherwords, hollandflora.com does not redirect to www.hollandflora.com, but instead just stays hollandflora.com and shows the content on hollandflora.com. (And it is not a DNS issue. Both A records point correctly.)

Any suggestions?

# January 31, 2010 10:52 PM

OWScott said:

Hi David.  My best guess is that another rule catches first and is set to stop processing.  That will prevent rules further down from working.

A good test to see if the conditions work is to set the action to a custom response code of 500 0 and Test / Test for the text.  That's a quick and easy way to confirm that it's working.  Try moving the rule to the top of the list and see if it catches then.  If it still doesn't, try a quick 'break test' by stopping the website to confirm that it's bound to that site (it sounds like it is).  Also confirm that the rule type it wildcards or regular expressions... whatever you're expecting.  Being set to the opposite as you expect could cause rules that don't work.

# February 1, 2010 6:00 PM

David Moore said:

Thanks for your help. It took reading through your examples a few more times and checking my code. Eventually I got it to work and now all my websites are humming with www redirected url's. Now on to SEO Friendly url's!? The new IIS 7(.5) is really, pretty incredible. It's just another learning curve for IT and web developers.

# February 25, 2010 4:22 PM

Twitter Mirror said:

http://weblogs. asp.net /owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

# March 27, 2010 4:43 PM

Steve Smith's Blog said:

I recently moved the DevMavens.com site from one server to another and needed to ensure that the www.devmavens.com domain correctly redirected to simply devmavens.com .&#160; This is important for SEO reasons (you don’t want multiple domains to refer

# March 29, 2010 11:28 AM

Mark Howell said:

You can do this without URL rewriting.  I do it this in IIS way:

Set up main site with host header for www.mydomain.com

Set up 2nd site with host header for mydomain.com

In IIS 6, In the Home Directory setting in the 2nd site properties, I tell the site to obtain content from a URL redirect to www.mydomain.com and click the option for making it a permanent redirection

In IIS 7, in the 2nd site HTTP Redirect properties, it set it to redirect to www.mydomain.com and select the option to make it a 301 permanent redirect.

I can set up a 3rd, 4th, .... nth site using different host header names, like mydomain.net, mydomain.us, etc., and configure them the same way as the 2nd site.

Then, whenever anyone uses the url for any of the 2nd or later sites, they are redirected to the first site.  

Works like a champ.

# January 26, 2011 12:32 PM

OWScott said:

Mark.  I fully agree, that works well in many situations.  Just a couple things to consider.  

Not everyone has a dedicated server.  Some people need to do this from a shared account.  So they don't have this as an option.

Some people have lots of domains so wildcard or regex rules offer more flexibility than setting up multiple IIS sites.

And some people would prefer (myself included) to use URL Rewrite rules--if they are familiar with URL Rewrite--over managing multiple redirecting sites.  It comes down to preference.

Definitely in IIS6 on a dedicated server, I agree fully.  Sometimes in IIS7 on a dedicated server, it all comes down to what people are familiar with.

# January 26, 2011 1:08 PM

chekmate111 said:

If I don't know how many numbers will be in the url can I just use * to account for all varitions?

Also, how do I add more than one rule to the Web.config? I would add the non-www to www rule and a rule that changed filename.aspx?id=11 to 11/filename.aspx

Thank you for the work you have done on this so far.  

# March 18, 2011 3:42 PM

OWScott said:

Hi chekmate111,

If your rule type is Regular Expressions, you can use \d for all digits.  Or \d{2} or \d\d to require 2 digits (for example).  .* will also work, but that gets all types of input, not just numbers.

You can add as many rules as you want.  It from the UI, just add another rule.  If from web.config, just wrap your rule in a <rule></rule> like the other one, and it will work without issue.

# March 18, 2011 4:41 PM

chekmate111 said:

OWScott thank you for your hard work!

Fundamental question: Which one of these two methods should I use?

1) write the code on my site to produce links in the form, /22/page.aspx and then redirect to page.aspx?id=22

or

2) write the link like page.aspx?id=22 and then redirect to /22/page.aspx ?

I ask because I see a lot of reference to redirecting to pagename.aspx?id=22 from 22/pagename

# March 21, 2011 11:56 AM

OWScott said:

Chekmate111,

To keep the search engines happy, you should make sure that your links on your pages (i.e. what your code generates) are in the form of /22/page.aspx.  Then do a 'rewrite' to page.aspx?id=22 using URL Rewrite.  Then the search engines are none the wiser about the page.aspx?id=22 format.  

So that's mostly your 1) option, but it's a rewrite rather than a redirect.  

You can still have a rule to redirect from page.aspx?id=22 to /22/page.aspx to catch anything that is missed in your HTML, but it's just an extra precaution.  It is a good practice though because the search engines will follow to the better URL and use that instead.

# March 21, 2011 12:15 PM

chekmate111 said:

Okay, so I think I am beginning to put this all together. I will code the links in like 22/pagename and rewrite to pagename?id=22

I will then try to redirect pagename?id=22 to 22/pagename just in case something gets overlooked.

Using your code above I believe I can redirect. Do you have a resource I could read for rewrite?

For the redirect I will try:

<rule name="somerulename" patternSyntax="Wildcard" stopProcessing="true">

                   <match url="*" />

                   <conditions>

                       <add input="{HTTP_HOST}" pattern="^/company-reviews.aspx?i=(\d)/" />

                   </conditions>

                    <action type="Redirect" url="/(\d)/company-reviews.aspx{R:0}" />

               </rule>

I hope I have that right. Not sure why this is so confusing.

# March 21, 2011 12:30 PM

OWScott said:

Chekmate111,

If you're using URL Rewrite 2.0, the easiest way to tackle this is to use the "User-friendly URL" wizard when adding a new rule.

Enter your URL as yourdomain.com/company-reviews.aspx

Select the pattern you want, for example:

yourdomain.com/.../123

And check the 2 checkboxes, which handle incoming and outgoing rules to automatically address when the HTML uses the original URL format.

It should generate something like this:

<rewrite>

   <rules>

       <rule name="RedirectUserFriendlyURL1" stopProcessing="true">

           <match url="^company-reviews\.aspx$" />

           <conditions>

       <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />

       <add input="{QUERY_STRING}" pattern="^i=([^=&amp;]+)$" />

           </conditions>

           <action type="Redirect" url="company-reviews/{C:1}" appendQueryString="false" />

       </rule>

       <rule name="RewriteUserFriendlyURL1" stopProcessing="true">

           <match url="^company-reviews/([^/]+)/?$" />

           <conditions>

               <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

           </conditions>

           <action type="Rewrite" url="company-reviews.aspx?i={R:1}" />

       </rule>

           </rules>

           <outboundRules>

       <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">

           <match filterByTags="A, Form, Img" pattern="^(.*/)company-reviews\.aspx\?i=([^=&amp;]+)$" />

           <action type="Rewrite" value="{R:1}company-reviews/{R:2}/" />

       </rule>

       <preConditions>

           <preCondition name="ResponseIsHtml1">

               <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />

           </preCondition>

       </preConditions>

   </outboundRules>

</rewrite>

Here's a blog post on rewriting:

weblogs.asp.net/.../iis-url-rewrite-hosting-multiple-domains-under-one-site.aspx

And here's a link to all of my blog posts on URL Rewrite.  The last four are video blogs which you may find more helpful to get up to speed.

weblogs.asp.net/.../default.aspx

# March 21, 2011 1:37 PM

chekmate111 said:

Wow. I guess I don't understand....

I tried:

<conditions>

<add input="{HTTP_HOST}" pattern="/([0-9]+)/company-reviews.aspx" />

</conditions>

<action type="Rewrite" url="company-reviews.aspx?id={R:1}"/>

To see if I could rewrite the url, but nothing happens. Didn't I put in the right variables? The current url is: www.incorporatealabamabusiness.com/.../company-reviews.aspx

which should be rewritten to www.incorporatealabamabusiness.com/company-reviews.aspx

# March 21, 2011 1:47 PM

chekmate111 said:

Thank you so much! I am going to share this page on my facebook.

# March 21, 2011 2:00 PM

OWScott said:

Hi chekmate111,

The path part of the URL is {URL}, or you can use the top section (match url="").

The {HTTP_HOST} is the domain name part.  If you watch this quick video, it covers the parts of the URL: weblogs.asp.net/.../url-rewrite-servervariables-url-parts-http-to-https-redirect-week-9.aspx

So what you probably want is:

<conditions>

<add input="{URL}" pattern="^([0-9]+)/company-reviews/?$" />

</conditions>

<action type="Rewrite" url="company-reviews.aspx?id={C:1}"/>

Or, you can do this instead (they accomplish the same thing):

<match url="^(0-9]+)/company-reviews/?$" />

<action type="Rewrite" url="company-reviews.aspx?id={R:1}" />

# March 21, 2011 4:43 PM

Jace said:

I followed the steps and it works great with the exception of the querystring. It does not pass the querystring on redirect. The "Append query string" is checked. What am I doing wrong?

# June 9, 2011 1:38 PM

OWScott said:

Hi Jace, that should work simply by checking that checkbox.  I wonder if there is another rule that causes the querystring to drop.  To test, try moving your rule to the top and making sure that other rules aren't affecting your test.

# June 9, 2011 1:44 PM

Jace said:

Thank you for the response.

It's actually the only rule I have on there. I'm using PHP instead of .Net with IIS. Would that cause an issue?

# June 9, 2011 2:08 PM

OWScott said:

Hi Jace.  PHP wouldn't affect it.  I'm not sure why it wouldn't work.  Some things to check would be the IIS logs and Failed Request Tracing.  Both of them should confirm that the querystring was there for the first pass and maybe/hopefully give a clue on why it gets dropped the 2nd time around.

# June 9, 2011 6:01 PM

web design blog said:

Well I only had FTP and Plesk access. Through pleask it is a headache. Anyway I downloaded the web.config and used the wild-card method using a text editor.  It worked. Thank you very much. Nevertheless I'm still having problems getting wordpress to work in IIS.

# June 22, 2011 5:31 AM

Chris Bowyer said:

Having only a small general knowledge of regular expressions and trying to get my head around URL Rewrite. I found your article more informative than many on the IIS website. I say this because you also advised on what things did. Thank you.

One thing I have not been able to find anywhere though, is a list of default settings e.g. It appears that there is no need to write <conditions logicalGrouping="MatchAll"> or appendQueryString="true" /> etc. Are you, or is anyone else aware of any reference to default settings?

In addition. Is there a tolerence in URL Rewrite for poorly formed regular expressions, like ^example.com$ which I believe, should be written ^example\.com$ and ^(.*)$ which seems overkill when (.*) is more than correct and adeqate to do the job, or am I wrong?

I have also played around with appendQueryString="false" and it appears to be useless e.g. <action type="Redirect" url="http://example.com/{R:1}" appendQueryString="false" /> still appends the query string. Can you, or anyone else provide an example of when this is essential?

# July 2, 2011 2:55 AM

OWScott said:

Hi Chris,

Good questions.  And thanks for the positive feedback.

Here's a quick tutorial video on how to understand the schema, which is a great way to find out all of the defaults:

dotnetslackers.com/.../Mastering-IIS-Understanding-the-Schema-Week-19.aspx

rewrite_schema.xml is the schema doc for URL Rewrite.

Regarding the poorly formed regular expressions: your first example is just more permissive.  For example, ^example.com$ will match exampleXcom too, but since it wouldn't exist in the real world in HTTP_HOST (unless you use host headers), it's not a problem to use either . or \. for the value.  I haven't tested performance for regex, but my guess is that \. is slightly faster because it's a literal match, but that’s just a guess.  The extra () around it is optional too.  They create sections which can be used in back references, but aren't needed if you don't use a back reference, or if you're matching everything.  I'm not aware of any performance differences one way or the other.  You're right that ^(.*)$ doesn't give you anything more than .* does except that it will offer one more back reference {R:1}, but since {R:1} will be the same as {R:0} it doesn't help any.  They both match everything.

As for appendQueryString, that one I don't know right off. I haven't experimented with it enough to know how it behaves.  My guess is the same as yours that it should append it when true, and not append it when false.  Do you have any other rules that are used instead?  Of course you could pull out the querystring in the redirect with a different rules, but that defeats the purpose of the appendQueryString attribute.

# July 5, 2011 11:55 AM

Chris Bowyer said:

Hi Scott,

Thanks for the heads up on the default settings. I have now added \Windows\System32\inetsrv\config\schema to my favourites for quick reference. Maybe someday, someone will create a cheat sheet. I do think the wizard could be improved though... i.e. deleting the defaults when changing e.g. ignoreCase="false" to ignoreCase="true". Mind you, although a bit programmatically clunky, it does help those not familiar with the defaults.

Also, thanks for the info on the poorly formed expressions. I have now finetuned a few things. Could you however, advise on {r:0} - is that a default back reference for when not enclosed in parentheses?

Still at stumps with the appendQueryString issue. Not that it is a major issue, but being an inquisitive type, I would love to eventually know what the go is.

# July 6, 2011 12:53 AM

OWScott said:

Hi Chris,

Yes, some of those settings are hard to handle perfectly in a UI.  For example, what do you do when you switch from a non-default value to a default value.  Do you delete the value or specifically set it.  UI's haven't been consistent over time on what to do in those situations.  Inheritance situations are even trickier (although fortunately not applicable in this case).

The {R:0} contains everything that *matched*. So this means that if you have a http_host of www.yourdomain.com and your regex value is "domain", then it will match, and {R:0} will be "domain" since that's all you searched for.  But, if you search for ".*domain.*" then it will catch the entire http_host and {R:0} will be "www.domain.com".  And, of course, "^domain$" won't even match, so {R:0} won't exist.

Check out week 10 and 11 from my series: dotnetslackers.com/.../LearnIIS7.  I cover most of this and more.

I'll do some testing on appendQueryString and see if I can repro what you're seeing.

# July 6, 2011 10:03 AM

OWScott said:

Hi Chris,

Regarding the appendQueryString, can you post your example of what doesn't work?  I just did a basic redirect with it set and without it and it does correctly honor that settings.  Although, I have heard other people say that it doesn't honor it too, so I don't doubt that it has an odd behavior in some situations.  

Also, make sure that you're not fighting with browser caching.  Try a fresh Chrome window in incognito mode to fight against caching.

# July 6, 2011 10:09 AM

Chris Bowyer said:

Hi Scott,

Thanks for the info on {R:0}. Very helpful!

I also checked out week 10 of your series. Very educational, but oops, week 11 is actually week 20

I also realised that I was thinking of the query string being page or directory/page. Anyway, I tested with an actual query string and voila, it worked as intended. Perhaps this is where others are going wrong too.

Just one other thing. Can you throw me any light on the <clear /> attribute?

# July 6, 2011 7:34 PM

OWScott said:

Hi Chris,

Thanks for mentioning about the wrong link.  I'll get that fixed.  In the meantime, here's the week 11 direct link: www.youtube.com/watch.

Great to hear that you've confirmed that the querystring attribute works correctly.  I'll keep that in mind when I hear of other similar issues.

The <clear /> element is used for collections.  Let's say that at the machine level you have a bunch of <handlers> set.  But at your site level you want to remove all references and start with your own choice of five (as an example).  The <clear /> removes all previous references for that section and allows you to start fresh.

In the case of URL Rewrite, if it's set at the site level, it removes all of the global rules ... although I don't believe it will help in that case since the global rules are applied before it processes the local <clear />.  I believe the <clear /> is just placed there as a standard practice to ensure that it's a clean start to avoid clashes of names.

# July 7, 2011 10:35 AM

Chris Bowyer said:

Hi Scott,

Thanks for all your help. In addition, the tutorials are very well done. I have bookmarked this page for future reference.

# July 7, 2011 5:41 PM

OWScott said:

Thanks Chris, and you're welcome.

# July 7, 2011 5:47 PM

ATaylor said:

Mark Howell's solution above is the correct one for just redirecting non-www to www (or vice versa, doesn't matter)

If that is all you are trying to do, just create another site (using host headers on both) and then use HTTP Redirection.

If you don't see HTTP Redirection in IIS 7.5, enable it by adding it to the Web Server Role services(in Server 2008).

Remember to prefix with your HTTP redirect with "http://" otherwise IIS will assume you are specifying a relative folder and you will get a loop.

For example:

to redirect foo.com to www.foo.com

1) ensure www.foo.com is a site with host header

2) make foo.com a site with host header

3) go into foo.com and in HTTP Redirect do the following in HTTP Redirect of foo.com site

Dialog Box in HTTP Redirect

Redirect to this location: [http://www.foo.com]

(remember to use the HTTP://)

Use neither check box presented below

AND select [Permanent - 301] as the code in the provided drop-down

Use Apply and all will be well

# July 17, 2011 12:17 PM

OWScott said:

Thanks ATaylor for the instructions.  This is a great reference for other people visiting this page.

Be sure to see my comments after Mark Howell's solution that give guidance on when to use a http redirect and when to use URL Rewrite.

One other thing to keep in mind with IIS7 and redirects is that they are delegated, so they are stored in the site's web.config file.  Therefore, it's essential that the new site points to a unique folder path.  It can't piggy back on the other site's site folder, otherwise the http redirect will end up getting applied to the other site too.

# July 18, 2011 10:11 AM

Tayyab Rana said:

Hi!

Thanks for this post. I really need this info but it does not work for me. I am redirecting to a a page without "WWW" and getting HTTP 404 not found error.

Please help me out what should I check and where should I debug. The web site is running on SharePoint 2007, Windows 2008

Thanks

# August 23, 2011 2:18 PM

OWScott said:

Hi Tayyab,

There are a few tips that can help troubleshoot something like this.  Here are some:

- make sure you can manually browse to the non-www URL.  That ensures that it's not something unrelated to URL Rewrite

- when the redirect occurs it will hopefully show the URL in the address bar.  That will offer large clues on what it's doing wrong.

- Try in IE and Chrome (or other browsers) because they respond differently and may show more information in the address bar.

- Check the IIS logs to see what pages are being hit after the redirect

- Use Failed Request Tracing which will provide details on the rules, conditions and actions.

# August 24, 2011 8:58 AM

Tayyab said:

I have checked every thing but nothing is unusual. Here is what I am doing.

<rewrite>

           <rules>

               <rule name="www-seo" patternSyntax="Wildcard" stopProcessing="true">

                   <match url="*" />

                   <conditions>

                       <add input="{HTTP_HOST}" pattern="Developer01/ABC" />

                   </conditions>

                   <action type="Redirect" url="http://www.google.com" appendQueryString="true" />

               </rule>

           </rules>

       </rewrite>

The redirect url is not displayed in the address bar.

I am keep getting 404 error.

This error (HTTP 404 Not Found) means that Internet Explorer was able to connect to the website, but the ....

# August 25, 2011 10:31 AM

Rick Noelle said:

Thanks for taking the time to write this up Scott. Sometimes a straightforward example like this is exactly what you need to get headed down the right path!

# August 27, 2011 12:56 PM

OWScott said:

My pleasure Rick.  Glad to help out.

# August 27, 2011 1:33 PM

OWScott said:

Hi Tayyab,

Sorry for the delay.  In your example you have {HTTP_HOST}.  That's for the domain name.  What you probably need is to use {URL} instead, or you can enter it as match url="^Developer01/ABC"

That's why it's not getting caught, and throwing a 404.

# August 30, 2011 11:35 AM

Penny said:

IIS URL Rewrite doesn't work in shared configuration?

# October 17, 2011 2:07 PM

OWScott said:

Hi Penny,

Are you referring to my reply to Mark Howell?  That was referring to using multiple sites to handle the redirect, which is often not provided with a shared provider.

URL Rewrite itself works beautifully with the shared configuration, and also on a shared host, as long as they have URL Rewrite already installed.

# October 17, 2011 2:16 PM

Vikas said:

Can anybody please share the procedure to 301 redirect all the non-WWW versions of the URLs on a domain name to their WWW equivalent please?

I have currently been able to set up a 301 redirect from the non-WWW version of the home page to its WWW equivalent. However, if I visit the non-WWW version of the URLs on the site, they all respond with a 200 OK code. The WWW versions of the URLs respond with a 200 OK code too.

I want to 301 redirect all the non-WWW versions of the URLs to their WWW equivalent.

Kindly share the procedure if anybody knows how to accomplish this.

Thanks,

Vikas

# December 21, 2011 9:13 AM

OWScott said:

Hi Vikas,

Good question and sorry for the delay.  I just put up a blog post for you that explains how to do that.  You can find it here: weblogs.asp.net/.../redirecting-non-www-to-domain-equivalent.aspx

# January 5, 2012 10:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)