4
votes

It seems with the built in friendly routing library in .NET MVC, it would allow us to do something like this.

In case it's not obvious what I want to with the built in stuff in .NET MVC, I want to a url starting with www to be automatically redirected to a non-www url using the MVC framework.

3
It's important to be aware that if you don't use a www (or some other subdomain) then all cookies will be submitted to every subdomain amd you won't be able to have a cookie-less subdomain for serving static content thus reducing the amount of data sent back and forth between the browser and the server. Something you might later come to regret: twitter.com/codinghorror/statuses/1637428313Sam Hasler
Complementing @Sam comment: For a little better understanding ravelrumba.com/blog/static-cookieless-domainBrunoLM

3 Answers

8
votes

You can use IIS 7 URL Rewriting module

You can setup it from IIS or just place in your web.config the following under <system.webServer>:

www to non-www

<rewrite>
  <rules>
    <rule name="Canonical" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
      </conditions>
      <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Alternatively you can make this redirection on global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs ev)
{
    if (Request.Url.Host.StartsWith("www", StringComparison.InvariantCultureIgnoreCase))
    {
        Response.Clear();
        Response.AddHeader("Location", 
            String.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Host.Substring(4), Request.Url.PathAndQuery)
            );
        Response.StatusCode = 301;
        Response.End();
    }
}

But remeber what @Sam said, look here for more info.


non-www to www

<rewrite>
  <rules>
    <rule name="Canonical" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^([a-z]+[.]net)$" />
      </conditions>
      <action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Make a regex pattern to match your host and use {C:0} 1, 2, ..., N to get the matching groups.

2
votes

There are a number of ways to do the 301 redirect from the www to the not-www. I prefer to keep this redirection logic at the ASP.NET level (i.e. in my app) in some projects, but others require better performing things, like IIS7 url rewriting.

It was discussed on the ASP.NET forums and I chose to use a WwwFilter on each controller. This has worked for me, no issues.

0
votes

try to add this into your Global.asax :

if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://YourSite.com"))
    {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://YourSite.com","http://www.YourSite.com"));
    }

it works and tested.