How to force example.com to be redirected to www.example.com with URL rewriting in IIS7? What kind of rule should go into the web.config? Thanks.
Asked
Active
Viewed 4.1k times
28
-
1duplicate of http://stackoverflow.com/questions/1476934/forwarding-http-mydomain-com-ctrlr-act-val-to-http-www-mydomain-com-ctrlr-act/1476986#1476986 – ax. Feb 05 '10 at 12:20
-
Why does it have to be URL rewriting? Why not the traditional approach: create a virtual host bound to `example.com` which gives a simple ‘HTTP Redirect’ to `http://www.example.com/` (with 301 status code, and without the ‘exact destination’ option, so the path stays intact). – bobince Feb 05 '10 at 12:36
-
I believe it is more logical to include this behavior within the application itself, rather than creating another one to support it. But otherwise there probably isn't any difference, or is there? – niaher Feb 06 '10 at 02:44
4 Answers
31
To make it more generic you can use following URL Rewrite rule which working for any domain:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

Atashbahar
- 551
- 5
- 13
-
-
-
@Serge A rule may have the StopProcessing flag turned on. When the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off. – Atashbahar Nov 29 '18 at 22:25
-
what is the point of turning it on? if you had another rule, say redirect `/*contact* => /contact-us` this rule would not be processed in that case – serge Nov 30 '18 at 10:31
-
28
This is Microsoft's sample for URL Rewrite Module 2.0 that redirects *.fabrikam.com to www.fabrikam.com
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.fabrikam.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.fabrikam.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

orad
- 15,272
- 23
- 77
- 113
-
3
-
-
If you want to preserve HTTPS protocol after adding the www part, this is a good solution - https://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action – Ross Oct 15 '16 at 01:26
-
1
3
Not sure about the best possible way to do this, but I have a site with all old domains / subdomains running this web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Transfer" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="http://www.targetsite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Seems to get the job done.

Sciolist
- 1,829
- 14
- 10
-
Wouldn't this just automatically redirect every request that comes to your server? I would expect your clients, if they inspected their traffic, to see a 301 for every request. – LeRoy Apr 06 '11 at 20:02
-
about a year late to answer your comment, but i have one iis app for www.targetsite.com, and another application that answers any domains i want forwarded. if one application is hosting both, it's of course neccessary to have a condition. – Sciolist Mar 29 '12 at 21:17
0
I'm not sure if this helps, but i opted to do this at the app level. 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
{
..
..
}
Hope that helps someone. Cheers!

Marc D.
- 99
- 1
- 3
-
Am sure this works but the sucky problem with this is that if you need to turn SSL off from IIS.. well.. you cannot. This for me is required to get new certificated from lets encrypt for example, just a quick rewrite turn off, get certs, turn back on. This way I need to republish. This is not very Polymorphic at all and considered bad practice. – Piotr Kula Apr 10 '17 at 11:52