IIS7 URL Rewrite - Add “www” prefix

后端 未结 4 1605
鱼传尺愫
鱼传尺愫 2020-12-03 16:57

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.

相关标签:
4条回答
  • 2020-12-03 17:16

    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>
    
    0 讨论(0)
  • 2020-12-03 17:28

    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!

    0 讨论(0)
  • 2020-12-03 17:32

    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.

    0 讨论(0)
  • 2020-12-03 17:42

    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>
    

    0 讨论(0)
提交回复
热议问题