Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS html5mode and hard 404

I have a AngularJS app working with html5mode set to true.

Currently, the app shows a soft 404 page, with the .otherwise setting in the router.

Is there a way I could serve actual 404 HTTP response, for the sake of SEO while using html5mode?

like image 764
marxo Avatar asked Nov 27 '25 17:11

marxo


1 Answers

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.

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.

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.

like image 181
Tiborg Avatar answered Nov 29 '25 08:11

Tiborg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!