xml IIS7 URL 重写 - 添加“www”前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:57:23  来源:igfitidea点击:

IIS7 URL Rewrite - Add "www" prefix

xmlurl-rewritingiis-7

提问by niaher

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.

如何通过 IIS7 中的 URL 重写强制将 example.com 重定向到 www.example.com?什么样的规则应该进入web.config?谢谢。

回答by Atashbahar

To make it more generic you can use following URL Rewrite rule which working for any domain:

为了使其更通用,您可以使用以下适用于任何域的 URL 重写规则:

<?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>

回答by orad

This is Microsoft's sample for URL Rewrite Module 2.0that redirects *.fabrikam.com to www.fabrikam.com

这是 Microsoft 的URL Rewrite Module 2.0示例,它将 *.fabrikam.com 重定向到 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>

回答by Sciolist

Not sure about the best possible way to do this, but I have a site with all old domains / subdomains running this web.config:

不确定执行此操作的最佳方法,但我有一个站点,其中所有旧域/子域都运行此 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.

似乎完成了工作。

回答by Marc D.

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.

我不确定这是否有帮助,但我选择在应用程序级别执行此操作。这是我编写的一个快速操作过滤器来执行此操作。只需在项目中的某处添加该类,然后您就可以将 [RequiresWwww] 添加到单个操作或整个控制器。

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!

希望能帮助某人。干杯!