5

I have an application written on ASP.NET MVC (V 1.0). The application runs on IIS7 and the DNS is provided by GoDaddy.

I would like to forward any request that comes from http://mydomain.example/ctrlr/act/value to a URL of this form: http://WWW.mydomain.example/ctrlr/act/value

Basically, I want to add WWW to the Host-name if someone tries to reach http://mydomain.example

What would be the best way to do this?

dove
  • 20,469
  • 14
  • 82
  • 108
xraminx
  • 1,176
  • 4
  • 13
  • 19
  • I think this is more of an IIS question. You should add the tag to draw the IISers' attention – Thiago Arrais Sep 25 '09 at 12:11
  • I won't file this as an answer, because this would require you to change your dev stack. But here is how I solved this same problem for my blog using Apache: http://pastie.org/630298. The only diference is that I'm prepending blog instead of www. Note also that the same server answers for both domain names in my case (don know if that's your case). – Thiago Arrais Sep 25 '09 at 12:21
  • @xraminx change of mind re answer below? – dove Sep 25 '09 at 12:31

3 Answers3

12

I think you will find an answer that suits from this question

I would agree with your idea of forcing the use off www, as though Stack Overflow decided to use it I do believe they regretted when tweaking performance for cookies and having to use sstatic.net instead of images.stackoverflow.com say.

To save you a redirect here is gist of what you need to do.

Here’s the IIS7 rule to add the WWW prefix to all incoming URLs. Cut and paste this XML fragment into your web.config file under

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

  <rule name="Add WWW prefix" >
    <match url="(.*)" ignoreCase="true" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="^domain\.com" />
    </conditions>
    <action type="Redirect" url="http://www.domain.example/{R:1}"
            redirectType="Permanent" />
  </rule>
jrummell
  • 42,637
  • 17
  • 112
  • 171
dove
  • 20,469
  • 14
  • 82
  • 108
1

You can use Url Rewriter from Code Plex. You can force everything to www.domain.example by doing the following:

RewriteCond %{HTTP_HOST} !^(www).*$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301]

Or if you want to make it more specific for your domain

RewriteCond %{HTTP_HOST} !^www.mydomain.example$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.example$1 [R=301]

This also supports a whole bunch of other rewriter functions provided by mod_rewrite.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
0

I opted to do this at the app level, instead of IIS. Here's a quick action filter I wrote to do this. Simply add the class somewhere in your project, and then you can add [RequiresWwww] to a single action or an entire controller.

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

            //IsLocal and IsLoopback i'm not too sure on the differences here, but I have both to eliminate local dev conditions.
            if (!req.IsLocal && !req.Url.Host.StartsWith("www") && !req.Url.IsLoopback)
            {
                var builder = new UriBuilder(req.Url)
                {
                    Host = "www." + req.Url.Host
                };

                res.Redirect(builder.Uri.ToString());

            }

            base.OnActionExecuting(filterContext);
        }
    }

Then

[RequiresWwww]
public ActionResult AGreatAction()
{
...
}

or

[RequiresWwww]
public class HomeController : BaseAppController
{
..
..
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Marc D.
  • 99
  • 1
  • 3