views:

118

answers:

3

I am attempting to force the domain name to not use the 'www'. I want to redirect the user if attempted. I have seen very little on an MVC solution. Is there anyway to harness the routing built into MVC, or what the best solutions is.

Thanks

+1  A: 

If you have control over the server, you should set up a virtual directory that accepts requests for "www.domain.com" and permanently redirects (301) them to "domain.com"

While this may be possible in ASP.NET MVC, it is not ASP's job to do this sort of redirecting.

On IIS: Virtual Directory

On Apache:

<VirtualHost *:80>
    ServerName www.domain.com
    Redirect permanent / http://domain.com/
</VirtualHost>

Both the IIS and the Apache settings will preserve the URL stem.

John Gietzen
Can you do this while preserving the users url. I want to drop the www transparently to the user, not just kick them back to root.
Dustin Laine
@durilai: If you use the settings in the screen shot I just added, you will get exactly that effect. This is how I have my server configured. For example, try going to http://www.lanlordz.com/Forums/
John Gietzen
I use discountasp.net, while they allow connecting to IIS manager I am unable to modify this. I would prefer this method, but do not think it is possible on shared host. Any thoughts?
Dustin Laine
+1  A: 

Implemented as an ActionFilter as that is MVC-like, and more explicit.

public class RemoveWwwFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var req = filterContext.HttpContext.Request;
        var res = filterContext.HttpContext.Response;


        var host = req.Uri.Host.ToLower();
        if (host.StartsWith("www.")) {
            var builder = new UriBuilder(req.Url) {
                Host = host.Substring(4);
            };
            res.Redirect(builder.Uri.ToString());
        }
        base.OnActionExecuting(filterContext);
    }
}

Apply the ActionFilter to your controllers, or your base controller class if you have one.

For an introduction to Action Filters, see Understanding Action Filters on MSDN.

[RemoveWwwFilterAttribute]
public class MyBaseController : Controller
Lachlan Roche
This will modify all request including script and stylesheet files, is there a way to just capture the views.
Dustin Laine
Why don't you use a `UriBuilder` for this, and use Request.Url?
John Gietzen
@John Agreed, that is is a better approach.
Lachlan Roche
Not sure the UriBuilder solves my initial problem with this method. Am I missing something?
Dustin Laine
@durilai ActionFilters apply only to those actions or controllers that you apply them to (they are Attributes).
Lachlan Roche
I have seen attribute method, but I feel it is not really the best solution. I would hate to have to add an attribute to every controller, not that it is difficult but I tend to side with the DRY methodology. I have found a decent solution in my answer. Thanks!
Dustin Laine
+3  A: 

Although I believe John Gietzen's answer is the most elegant solution, I was unable to implement do to a shared hosting environment. Determined to find a non-application driven solution I found this blog post which shows a good alternative method for IIS7. Thankfully DiscountASP.NET's has the URL Rewrite module available through the IIS Manager tool.

Following this blog post on creating a rewrite rule any URL with www in the domain will do a permanent 301 redirect to the non-www site. All while preserving the full paths.

Thanks for everyone's input.

Dustin Laine