最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I return a HTTP 404 status code from a SPA? - Stack Overflow

programmeradmin3浏览0评论

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

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

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.

However, right now Google tagged this site as "being hacked" since EVERY URL returns an HTTP 200 status code (example/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.

A few ideas came to my mind:

  • 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.

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

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

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.

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.

A few ideas came to my mind:

  • 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.

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked May 19, 2016 at 21:21 MauroMauro 4,2242 gold badges31 silver badges41 bronze badges 5
  • Seeing as how I got downvoted for adding text into the wrong box, I suppose I should leave a comment instead... Did you ever make any headway on this issue? We have the same problem. – Matt Crysler Commented Jun 23, 2016 at 14:51
  • Not really. I ended up doing what the first option says. – Mauro Commented Jun 23, 2016 at 16:26
  • Have you noticed a decrease in performance using that method then? – Matt Crysler Commented Jun 23, 2016 at 16:36
  • Very Slight, since both boxes are on AWS's same region, I'm doing a HEAD request instead of GET and I adjusted the API to just poke the database for the record. – Mauro Commented Jun 23, 2016 at 16:38
  • 2 Another possible solution to your problem: stackoverflow.com/a/53400921/220086 – Denis Pshenov Commented Nov 20, 2018 at 22:36
Add a comment  | 

2 Answers 2

Reset to default 12

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.

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 !!!

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 follows.

One way is to set <meta name="robots" content="noindex" /> in the head using javascript. Just be sure to remove it when navigating to a real page after. I found this solution discussed at Googles Search Console Help, apparently setting noindex is how they did it at angular.io.

发布评论

评论列表(0)

  1. 暂无评论