37

I am trying to get Laravel 5 (5.1.31) to return a http status response of 404 when a page is not found. I have tried several things and it always returns 200.

In my controller I have this:

else
   {
   header('HTTP/1.0 404 Not Found');
   return view('errors.404);
   }

I have also tried:

else
   {
   http_response_code(404);
   return view('errors.404);
   }

and

else
   {
   abort(404, 'Page not found');
   }

I also tried putting this in the 404.blade

@inject( 'response', 'Illuminate\Http\Response' )
{{ $response->status(404) }}

No success. No matter what I try, Laravel returns 200. How do I get it to return 404?

4
  • laravel.com/docs/5.1/errors . Where do you see the code 200? In the developer toolbar? Commented Mar 5, 2017 at 21:30
  • In the browser when I am loading the page, laravel returns 200 for the status code. I need it to return 404 for page not found, this is especially important for the search engine spiders so that they know when they have reached a bad address. Commented Mar 5, 2017 at 22:05
  • 1
    abort(404) should work. Is the else part of your if statement definitely getting executed? Commented Mar 5, 2017 at 23:13
  • It is definitely executing the abort(404,"message") as it displays the message on the 404 page, however it is still sending a 200 status code to the browser. Commented Mar 6, 2017 at 14:57

6 Answers 6

90

you may use the abort helper:

abort(404);
Sign up to request clarification or add additional context in comments.

4 Comments

No, it MUST NOT BE the accepted answer. This answer not only doesn't address the question, it shows the person answering didn't read the question, because that non-solution is already addressed in the question. In short, the answer does not work, and if the person who wrote that answer would have read the question, they would have known it doesn't work.
@DanWillett don't be so salty mate. as a random user coming from "google: laravel return 404 view" this answere is totally valid. could not care less about some $data variable. classical case of "what's good for me vs. what's good for everybody else"
and hint: i did never say "answere is correct" i only said "don't be so salty"
@clockw0rk I am glad you found the information you were looking for. I would point out that the $data is not the part that addresses the core issue raised by the original question. The core issue, which many people apparently have a hard time understanding is that the response being returned to the browser / web spider was returning a status code of 200 success instead of the correct 404 error in the header which was causing search engines to index non-existent pages instead of correctly recognizing that the URL was invalid by it correctly returning a 404 error code in the header.
27

While I don't know why abort is not returning the 404 status as it is suppose to, I did find a solution that will make Laravel return a 404 status:

Here is what I did:

else {
    $data['title'] = '404';
    $data['name'] = 'Page not found';
    return response()->view('errors.404',$data,404);
}

This actually works better for my purposes because it doesn't mess with the contents of my 404.blade like the abort does.

3 Comments

an how to call in the view with blade?
@Sebastián I was not able to get this to work from the view. This has to be put in the controller.
This works for Laravel 5.4 - @DanWillett - I accidentally hit downvote & the system refuses to allow me to undo this - sorry!! Will see if this can be undone.
17

Very simple, I assumed you use Laravel v5++ just go to

app > Exceptions > Handler.php

See the picture as below:

enter image description here

And modify the codes from:

public function render($request, Exception $e)
{
    return parent::render($request, $e);
}

to

public function render($request, Exception $e)
{
    if ($e instanceof MethodNotAllowedHttpException)
    {
        abort(404);
    }
    return parent::render($request, $e);
}

Then, do not forget to add 404.blade.php page in errors folder:

enter image description here

Well, you can customise by yourself the 404 page in 404.blade.php.


Note

This case only when you run the URL were not listed as in the routes. You may find in web.php file.

In case you need to call by custom, just call in the controller like below:

public function show_me()
{
   abort(404);  //404 page
}

Hope it helps!

1 Comment

Already have the /errors/404.blade.php. Have already tried using the abort(404) in my controller. It is returning the correct 404 blade, but it is still sending status code 200 to the browser instead of the correct 404 error code. You may wish to read the entire question as all of this information is in the original question as well as to what version of Laravel I am running...
3

If you want to return JSON instead of a view, you can call this in your controller:

return response(['error'=>true,'error-msg'=>$msg],404);

1 Comment

The question has nothing to do with JSON.
0

You can also do this

return response(view('view_name'), 404);

You can also add headers to the response like this

return response(view('view_name'), 404, [
    'header_1' => 'header 1 value',
    'header_2' => 'header 2 value' ...
]);

Extra info

The thing is that it works both on Laravel and Lumen (micro framework by Laravel). However,

response()->view() 

doesn't work on Lumen.

Comments

-1

This is what I did because I needed to use my custom view and a 404 header:

return response()
            ->view('my_view', $data, 404);

I found it on https://laravel.com/docs/8.x/responses#view-responses

It worked in Laravel 8, but it seems that it works in older versions.

2 Comments

If you want custom view, then just create resources/views/errors/404.blade.php.
As mentioned in my comment, I needed to use my custom view and a 404 header. Instead of the default custom view. I needed to leave the default as it is and use mine just in that response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.