代码之家  ›  专栏  ›  技术社区  ›  jp2code

在托管ASP网站的URL中附加www(即没有IIS访问权限)

  •  2
  • jp2code  · 技术社区  · 13 年前

    我看到许多类似的问题都是用我不理解的胡言乱语写的:

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

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

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

    2 回复  |  直到 9 年前
        1
  •  6
  •   coder    13 年前
    1. 非www到www重定向
    2. www.yourdomainname.com/default.aspx至www.yourdomainname

    现在在web.config中添加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>
    
        2
  •  3
  •   Brian Webster Jason    13 年前

    这是一个有趣的解决方案。我之所以提出这个建议,只是因为你所描述的局限性。最好在IIS中进行,或者像其他答案建议的那样使用HTTP模块。然而,这也是可行的,只是这不是一个很好的方法。

    您可以将此代码放入页面初始化事件处理程序(或主页面的初始化处理程序)。

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

    哪里 mydomain 就像 mydomain.com 没有 www .

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

    推荐文章