如何从SPA返回HTTP 404状态代码? [英] How do I return a HTTP 404 status code from a SPA?

查看:111
本文介绍了如何从SPA返回HTTP 404状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了几个这样的问题(比如这个),但他们都没有具体解决这个问题。

I saw a few questions like this around (like this one), but none of them tackle the problem specifically.

因此Google 现在支持SPA 大多数网络浏览器执行HTML5 pushState

So Google is now supporting SPAs and most web browsers do HTML5 pushState.

我的AngularJS(但可能是任何JS的东西)网站正在使用URL来确定API路由。然后它执行API调用,然后相应地呈现内容。

My AngularJS (but could be any JS thing) website is using the URL to determine an API route. It then performs the API call and then renders the content accordingly.

但是,现在Google将此网站标记为被黑客入侵,因为每个网址返回HTTP 200状态代码(包含example.com/get-free-viagra)。公平,但我如何归还404?或者至少告诉Google这是一个未找到的页面?他们似乎没有提供这些信息,我非常担心SEO。

However, right now Google tagged this site as "being hacked" since EVERY URL returns an HTTP 200 status code (example.com/get-free-viagra included). Fair, but how do I return a 404? Or at least inform Google that this is a not-found page? They don't seem to be providing that information and I'm seriously worried about SEO.

我想到了一些想法:


  • 弃用我当前的设置(我使用AWS S3来托管静态网站),然后使用expressJS框,使用可以执行API调用并返回的中间件404如果需要的话。但是,我不喜欢这种方法,因为它会损害性能(每个前端请求有两个API调用)。

  • 使用 window.location 重定向到适当的404页面。但是,我不确定Google是否会关注它,并且已经不鼓励更改网址。

  • 使用 rel =nofollow在未找到的页面上,但我觉得这还不够。

  • Deprecate my current setup (I'm using AWS S3 to host the static website), and use an expressJS box instead, with a middleware that would perform the API call and return the 404 if needed. However, I don't like the approach since it will harm performance (two API calls per frontend request).
  • Use window.location to redirect to a proper 404 page. However, I'm not sure if Google will follow it and it's already discouraged to change the URL.
  • Use rel="nofollow" on not found pages, but I don't feel this is enough.

我现在正沮丧地倾向于第一个选项权利现在。

I'm now frustratingly leaning towards the first option right now.

推荐答案


使用window.location重定向到正确的404页面。但是,如果Google会关注它并且我们不鼓励更改
的网址,我不会花钱。

Use window.location to redirect to a proper 404 page. However, I'm not sure if Google will follow it and it's already discouraged to change the URL.

你的假设不正确。如果您重定向到适当的404页面(或410),Google将非常好。谷歌将遵循它,并将非常满意这些信息。它想知道伪造的URL,以确保它们不会被包括在他们的排名中。他们会喜欢它!

Your assumption is not correct. Google will be very fine if you redirect to a proper 404 page (or a 410). Google will follow it and will be very happy with this information. It wants to know about bogus URLs to make sure these won't be included in their rankings. They will love it !!!

作为提醒,虽然它不是执行重定向的首选方式,但Google接受并关注具有Refresh标记并且延迟设置为0的页面,因为,在一些棘手的情况下,没有其他方法可以执行重定向。这是Blogger页面(由Google拥有)的推荐方法。

As a reminder, and although it is not the preferred way to perform a redirect, Google accepts and follows pages having a Refresh tag with its delay set to 0, because, in some tricky cases, there is simply no other way to perform a redirect. This is the recommended method for Blogger pages (owned by Google).

Google跟随。

这篇关于如何从SPA返回HTTP 404状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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