MVC中的[RequireHttps]是否不应该执行301永久重定向?为什么要执行302(对SEO不利?) [英] Shouldn't [RequireHttps] in MVC do a 301 permanent redirect? Why does it do a 302 (bad for SEO?)

查看:108
本文介绍了MVC中的[RequireHttps]是否不应该执行301永久重定向?为什么要执行302(对SEO不利?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提琴手上注意到[RequireHttps]会重定向状态代码302,而不是301.我不确定这有什么意义...

I noticed on fiddler that [RequireHttps] does status code 302 redirects instead of 301. I'm not sure how this makes sense...

如果您说的是控制器[RequireHttps],那么您永远不会希望人们去访问该页面的Http版本.那么为什么不是永久重定向...告诉搜索引擎请永久将您的链接更新到此页面的https版本".

If you are saying that a controller [RequireHttps], then you never-ever want people to go visit the Http version of that page. So why isn't a permanent redirect... telling the search engines "please update your links permanantly to the https version of this page".

如果这是有道理的,而我是对的,是否可以将其更改为301重定向?

If this makes sense, and i'm right, is there a way to change it to 301 redirect?

推荐答案

开始时似乎选择302而不是301.但是,并不一定要每个URL都拥有"以利用HTTPS方案.可能会有一个页面允许同时从HTTP或HTTPS进行访问,即使它可能鼓励后者.可能发生这种情况的实现可能需要连接一些代码,以便根据某些特殊条件来确定是否使用HTTPS.

It seems as if the choice to go with 302 over 301 was a little arbitrary to begin with. However, it does not necessarily follow that every URL is going to "have" to utilize the HTTPS scheme. There very well could be a page that allows access from both HTTP or HTTPS even if it may encourage the latter. An implementation where this may occur could have some code wired up to determine whether or not to use HTTPS based on some special criteria.

作为一个案例,请看一下Gmail.在这些设置中,可以在整个应用程序的大部分范围内允许或禁止HTTPS协议.那应该返回哪个代码? 301不是准确的,因为它不是永久的" ...仅应用户要求进行更改.遗憾的是,302也不太准确,因为302错误表示将来有意将链接更改回原来的位置(相关参考资料http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html ).

As a case scenario, take a look at Gmail. Within the settings, one is capable of allowing or disallowing the HTTPS protocol throughout large portions of the application. Which code should be returned then? 301 wouldn't be accurate, as it isn't "permanent"...only a change at the behest of the user. Sadly, 302 isn't quite accurate either because a 302 error implies that there is intent to change the link back at some point in the future (related reference http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).

Gmail就是一个粗糙的例子,因为网站上允许该选项的部分通常不会被搜索引擎索引,但是可能性仍然存在.

Granted, Gmail is a rough example because the portions of the site that allow that option are not typically indexed by a search engine, but the possibility still exists.

并回答您的最后一个问题,如果您想在ASP.NET MVC中使用不同的状态代码(我假设您是从小型语法示例中使用的),则可以使用简单的自定义属性进行更改:

And to answer your final question, if you want a different status code in ASP.NET MVC (which I assume you're using from the small syntax example), it is possible to change with a simple, custom attribute:

public class MyRequireHttpsAttribute : RequireHttpsAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);

        if (!filterContext.HttpContext.Request.IsSecureConnection)
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
    }
}

现在,通过HTTP协议访问时,实现该属性的所有操作都应返回301状态代码.

Now all actions that implement the attribute should return a 301 status code when accessed via the HTTP protocol.

这篇关于MVC中的[RequireHttps]是否不应该执行301永久重定向?为什么要执行302(对SEO不利?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆