Drop User to custom 404 page

Hello,

I have created a custom 404.html page and redirect users when they open any routes that are not available.

I want to redirect user to this 404 page after I receive data from backend. e.g I have an API called getPostData() and it returns a variable called is_post_expired.

if(response.data.is_post_expired===true){
                    window.location.href = "/not-found";
                }

and in _redirects file i have added

/not-found /404.html 404!

Is there any way i can return 404 without changing the url?. The URLs uses posting id which is dynamic .

This video resource might help:

My api is called from react component. When I receive the response I check if the post is expired or not. I redirect to 404 if it is expired and load the post if the post is available.

In case of post expired it redirect to https://example.com/not-found and return 404. I want it to return it 404 without changing the initial URL (https://example.com/post/randon-post-id).

That sounds like something that you’ll need to configure in react, actually. Option 2 in this resource

This will returns a 200 to the client and search engine. How can you return a status code 404?