views:

78

answers:

2

Hello, I would like to redirect all www traffic to non-www traffic

i have copied this into my web.config

<system.webServer> / <rewrite> / <rules> 

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

per this post

http://stackoverflow.com/questions/521804/how-to-redirect-with-www-urls-to-without-www-urls-or-vice-versa/521811#521811

but I got a 500 internal server error.

+2  A: 

if you copied it directly then you have incorrect markup in your web.config

you need

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

The line that says

<system.webServer> / <rewrite> / <rules> 

is stating that you need to put the config in that location within your web.Config.
<system.webServer> is one of the configSections of your web.Config file.

EDIT:

Make sure you first have the URL Rewrite module installed for IIS7

The page above talks about redirecting HTTP to HTTPS, but the concept still applies for WWW to non WWW

Also, here is some detailed information on how it all comes together.

rockinthesixstring
ok..i have done this and it says <rewrite> is not a valid tag
Luke101
lemme look into it.
rockinthesixstring
I edited my answer. I'm still looking how to make it so that Visual Studio doesn't yell at you, but it should work for IIS7
rockinthesixstring
+2  A: 

You might consider a different approach:

protected void Application_BeginRequest (object sender, EventArgs e)
{
   if (!Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
   {
      UriBuilder builder = new UriBuilder (Request.Url);
      builder.Host = "www." + Request.Url.Host;
      Response.Redirect (builder.ToString (), true);
   }
}

This will however do a 302 redirect so a little tweak is recommended:

protected void Application_BeginRequest (object sender, EventArgs e)
{
   if (!Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
   {
      UriBuilder builder = new UriBuilder (Request.Url);
      builder.Host = "www." + Request.Url.Host;
      Response.StatusCode = 301;
      Response.AddHeader ("Location", builder.ToString ());
      Response.End ();
   }
}

This one will return 301 Moved Permanently.

Developer Art
does this approach work in MVC? Your way is how I do it in Web Forms, but I think the MVC Routing Framework is dealt with differently.
rockinthesixstring
I took a very similar approach. See http://stackoverflow.com/questions/2175975/asp-net-mvc-301-redirect-from-www-domain-com-to-domain-com
spender
@rockinthesixstring: It works in MVC. It kicks in very early so it doesn't matter much whether MVC or WebForms is going to be processing the request afterward.
Developer Art
where would I put this code in the project?
Luke101
It goes in Global.asax.cs.
GalacticCowboy