简体   繁体   English

AngularJS html5mode和404

[英]AngularJS html5mode and hard 404

I have a AngularJS app working with html5mode set to true . 我有一个AngularJS应用程序使用html5mode设置为true

Currently, the app shows a soft 404 page, with the .otherwise setting in the router. 目前,该应用程序显示一个软404页面,在路由器中具有.otherwise设置。

Is there a way I could serve actual 404 HTTP response, for the sake of SEO while using html5mode? 有没有一种方法可以提供实际的404 HTTP响应,为了SEO而使用html5mode?

If I understand correctly what you want, you have to do the following: 如果我理解你想要什么,你必须做以下事情:

  1. hard redirect the browser (bypassing the angular routing) on the otherwise path, with something like this: 硬重定向浏览器(绕过角度路由)在其他路径上,用这样的东西:

     $routeProvider .otherwise({ controller: function () { window.location.replace('/404'); // your custom 404 page // or a non existing page } }); 
  2. if you have a regular single-page-application where all the server request are redirected to the same SPA entry point, you have to configure on your server to make a routing exception for your custom 404 page, which will should also be served with a 404 status. 如果您有一个常规的单页面应用程序,其中所有服务器请求都被重定向到相同的SPA入口点,您必须在服务器上配置为自定义404页面制作路由例外,该页面也应该与404状态。

Otherwise, I can't see how you would do that with just History API, without an external request, because it's whole point in angular routing is to bypass external requests. 否则,在没有外部请求的情况下,我无法看到你如何使用History API,因为角度路由的全部意义是绕过外部请求。

If you just want non-existing routes to return 404, then you must configure your server to match all your angular routes, and return 404 otherwise. 如果您只希望不存在的路由返回404,则必须将服务器配置为匹配所有角度路由,否则返回404。

Seach engines works with SPA applications through prerendered pages, using _escaped_fragment_ . Seach引擎使用_escaped_fragment_通过预渲染页面与SPA应用程序一起使用。 And you can use Prerender.io (or simply PhantomJS) to generate any status codes for search engines, like this https://prerender.io/documentation/best-practices 您可以使用Prerender.io(或简称PhantomJS)为搜索引擎生成任何状态代码,例如https://prerender.io/documentation/best-practices

But this schema is deprecated by Google: http://googlewebmastercentral.blogspot.ru/2015/10/deprecating-our-ajax-crawling-scheme.html At this moment Google tries to understand your JS with usual crawling schema. 但Google不推荐使用此架构: http//googlewebmastercentral.blogspot.ru/2015/10/deprecating-our-ajax-crawling-scheme.html此时Google尝试通过常规抓取架构了解您的JS。

Hard redirection to 404.html page is not a good practice: url must stay the same, like https://stackoverflow.com/somepage 硬重定向到404.html页面不是一个好习惯:url必须保持不变,例如https://stackoverflow.com/somepage

You can try Angular2 with server rendering feature: https://docs.google.com/document/d/1q6g9UlmEZDXgrkY88AJZ6MUrUxcnwhBGS0EXbVlYicY/edit 您可以尝试使用服务器呈现功能的Angular2: https ://docs.google.com/document/d/1q6g9UlmEZDXgrkY88AJZ6MUrUxcnwhBGS0EXbVlYicY/edit

You have to make your server issue 404s. 您必须使您的服务器问题404s。 Angular cannot help in anyway here. 无论如何,Angular在这里无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM