【问题标题】:IIS7 URL Rewrite - Add "www" prefixIIS7 URL 重写 - 添加“www”前缀
【发布时间】:2011-01-13 12:06:49
【问题描述】:

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

【问题讨论】:

  • 为什么一定要重写URL?为什么不采用传统方法:创建一个绑定到example.com 的虚拟主机,它提供了一个简单的“HTTP 重定向”到http://www.example.com/(带有 301 状态代码,并且没有“确切目的地”选项,因此路径保持不变)。
  • 我相信将这种行为包含在应用程序本身中更合乎逻辑,而不是创建另一个来支持它。但否则可能没有任何区别,或者有吗?

标签: xml url-rewriting iis-7


【解决方案1】:

不确定执行此操作的最佳方法,但我有一个站点,其中包含运行此 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>

似乎完成了工作。

【讨论】:

  • 这不会自动重定向到达您服务器的每个请求吗?如果您的客户检查他们的流量,我希望他们会看到每个请求的 301。
  • 回复您的评论大约晚了一年,但我有一个适用于 www.targetsite.com 的 iis 应用程序,还有另一个应用程序可以回答我想要转发的任何域。如果一个应用程序同时托管两者,当然需要有条件。
【解决方案2】:

这是 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>

【讨论】:

  • 我想补充一点,您可以使用 fabrikam.com{R:1}" /> 将其设为 301 永久重定向跨度>
  • www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
  • 如果你想在添加www部分后保留HTTPS协议,这是一个很好的解决方案-weblogs.asp.net/owscott/…
  • 如何从规则中排除子域?
【解决方案3】:

为了使其更通用,您可以使用以下适用于任何域的 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>

【讨论】:

  • 只要您没有子域就可以使用。
  • 为什么stopProcessing="true"
  • @Serge 规则可能已打开 StopProcessing 标志。当规则动作被执行(即规则匹配)并且该标志被打开时,意味着将不再处理后续规则,并且请求将被传递到IIS请求管道。默认情况下,此标志是关闭的。
  • 打开它有什么意义?如果您有其他规则,例如重定向 /*contact* =&gt; /contact-us,则在这种情况下不会处理此规则
  • 这会重写 https:// 吗?
【解决方案4】:

我不确定这是否有帮助,但我选择在应用程序级别执行此操作。这是我为此编写的一个快速操作过滤器。只需在项目中的某处添加类,然后您可以将 [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);
        }
    }

然后

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

[RequiresWwww]
public class HomeController : BaseAppController 
{
..
..
}

希望对某人有所帮助。干杯!

【讨论】:

  • 我确信这是可行的,但一个糟糕的问题是,如果你需要从 IIS 关闭 SSL .. 好吧.. 你不能。这对我来说是从 Let encrypt 获取新证书所必需的,例如,只需快速重写关闭、获取证书、重新打开。这样我需要重新发布。这根本不是多态的,被认为是不好的做法。
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 2012-04-10
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
相关资源
最近更新 更多