ASP.net MVC网站重定向所有“非WWW”请求WWW(ASP.net MVC site : R

2019-08-21 23:10发布

Recently I migrated an ASP.net site to ASP.net MVC site. Earlier there were two host headers one mydomain.com and another is www.mydomain.com. My SEO says you should use only one url "www.domain.com" for SEO advantage.

I am looking for an option to do 301 permanent redirect all mydomain.com request to www.mydomain.com.

The site is hosted in IIS6 and developed in ASP.net MVC 4.

Answer 1:

不幸,URL重写模块不IIS6(仅IIS7或更高) 的工作 。 你有没有考虑创建自己的HttpModule,这样的事情呢?

IIS 6如何从http://example.com/*重定向到http://www.example.com/*

或者你可能会使用像其中的一个第三方的解决方案:

http://iirf.codeplex.com/

http://www.urlrewriting.net/149/en/home.html

http://www.isapirewrite.com/

http://urlrewriter.net/



Answer 2:

您可以从您的web.config文件中做到这一点

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


Answer 3:

你可以使用配置或URL重写在IIS中,但我已经找到了最好的方法就是把一些代码Application_BeginRequest()在你global.asax.cs这样的:

var HOST = "www.mydomain.com";

if ( !Request.ServerVariables[ "HTTP_HOST" ].Equals(
  HOST,
  StringComparison.InvariantCultureIgnoreCase )
)
{
  Response.RedirectPermanent(
    ( HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://" )
    + HOST
    + HttpContext.Current.Request.RawUrl );
}

因为你在代码中这样做,你可以有你需要在每个请求的任何逻辑。



Answer 4:

(IIS 7或更大的需要)

从http://www.codeproject.com/Articles/87759/Redirecting-to-WWW-on-ASP-NET-and-IIS

(以上述方案类似,但不要求你添加你自己的域名。)

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="WWW Rewrite" enabled="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" negate="true"
                            pattern="^www\.([.a-zA-Z0-9]+)$" />
                    </conditions>
                    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
                        appendQueryString="true" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

请注意,您很可能会看到波浪线的标签下有一条消息,该标签是无效的。 我得到这个消息,但事实上,它的工作就好了。

如果你想在智能感知工作,你可以在这里试试这个更新中...

http://ruslany.net/2009/08/visual-studio-xml-intellisense-for-url-rewrite-1-1/

有关httpRedirect更多信息可以在这里找到...

http://www.iis.net/configreference/system.webserver/httpredirect



文章来源: ASP.net MVC site : Redirect all “non WWW” request to WWW