2

我看到很多类似的问题都是用乱码写的,我不明白:

我想知道如何使用微软的技术来做到这一点......或者只是向我解释其他人在谈论什么以及如何使用它们。

基本上,如果有人在地址栏中键入“mydomain.com”,我希望它在页面完成加载后解析为“www.mydomain.com”。

编辑:这是一个托管网站,所以我无法配置 IIS 服务器。

4

2 回答 2

6
  1. 非 www 到 www 重定向
  2. www.yourdomainname.com/default.aspx 到 www.yourdomainname.com

现在在 web.config 中添加配置标签

<system.webServer>
<rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^yourdomainname.com$" />
          </conditions>
          <action type="Redirect" url="http://www.yourdomainname.com/{R:0}" redirectType="Permanent" />
        </rule>

        <rule name="Default Document" stopProcessing="true">
          <match url="(.*?)/?default\.aspx$" />
          <action type="Redirect" url="{R:1}/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

(或)选择这个:

<rewrite>
    <globalRules>
        <rule name="Redirects to www.domain.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^domain.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?mydomain2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^www.domain.net$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>
于 2013-02-23T03:17:17.793 回答
3

这是一个有趣的解决方案。我只是因为你描述的限制而建议它。最好在 IIS 中执行此操作,或者像其他答案所建议的那样使用 HTTP 模块。但是,这也可以,但这不是实现它的好方法。

您可以将此代码放入您的 Page Init 事件处理程序(或母版页的 Init 处理程序)。

If Request.RawUrl.StartsWith("http://mydomain") Then
  Response.Redirect(Request.RawUrl.Replace("://", "://www."))
End If

没有. mydomain_ mydomain.com_www

它检查 URL 是否没有 WWW “应该”在的地方。如果它不存在,则将用户重定向到在正确位置有 WWW 的那个页面的版本。

于 2013-02-23T03:13:16.677 回答