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

查看:34
本文介绍了如何从 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.

但是,由于每个网址都会返回 HTTP 200 状态代码(包括 example.com/get-free-viagra),因此现在 Google 将此网站标记为被黑客入侵".公平,但我如何返回 404?或者至少通知谷歌这是一个未找到的页面?他们似乎没有提供这些信息,我很担心 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 是否会遵循它,并且已经不鼓励更改 URL.
  • 在未找到的页面上使用 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 页面.然而,我不是确定谷歌是否会遵循它并且已经不鼓励改变网址.

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,以确保这些 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 接受并跟踪具有延迟设置为 0 的 Refresh 标记的页面,因为在某些棘手的情况下,根本没有其他方法可以执行重定向.这是 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).

谷歌关注.

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

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