如何在ASP.NET MVC 4上从非削减根目录重定向到削减根域

时间:2013-02-03 17:04:14

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 asp.net-mvc-4 asp.net-mvc-routing

拥有域http://mosaically.comhttp://mosaically.com/的ASP.NET MVC 4网站,两者都返回200 OK响应。我希望mosaically.com能够将SEO重定向到mosaically.com/,因为搜索引擎优化的原因是斜线。在MVC 4中有没有办法做到这一点?我宁愿在MVC中这样做,而不是尝试使用IIS。

3 个答案:

答案 0 :(得分:0)

您可以使用IIS中的Url Rewrite module来完成此任务。这是一个nice step by step article,说明了如何启用它。以下是web.config中样本重写规则的外观:

<system.webServer> 
    <rewrite> 
        <rules> 
            <rule name="CanonicalHostNameRule1"> 
                <match url="(.*)" /> 
                <conditions> 
                    <add input="{HTTP_HOST}" pattern="^www\.mosaically\.com$" negate="true" /> 
                </conditions> 
                <action type="Redirect" url="http://www.mosaically.com/{R:1}" /> 
            </rule> 
        </rules> 
    </rewrite> 
</system.webServer> 

答案 1 :(得分:0)

刚发现它无法完成,即使你是查克诺里斯说谷歌

http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html

“请放心,对于您的根URL,example.com相当于example.com/,即使您是Chuck Norris,也无法重定向。”

答案 2 :(得分:0)

我认为你应该像那样使用

你的global.aspx

    protected void Application_BeginRequest()
        {
            string rudsiteurl = HttpContext.Current.Request.Url.AbsoluteUri;
            if (rudsiteurl.Contains("http://www.mosaically.com"))
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", "http://www.mosaically.com/");
            }

        }

我认为这会对你有帮助......

相关问题