The requirement is to redirect to 404 page when data passed on URL query string is not correct. So in the controller, I get the data from URL and check if it is correct, it can proceed. And What should I do when it is incorrect, it should redirect to 404 page?

2 Answers 2


There are some methods discussed on different forums but the one worked for me is as follow.


namespace Company\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Context;
use \Magento\Framework\Exception\NotFoundException;

class Index extends \Magento\Framework\App\Action\Action {
    public function execute() {
        throw new NotFoundException(__('Parameter is incorrect.'));                 

This will throw an exception and return a 404 not found page.


You can use this code for redirect to 404 page all pages, It is working definitely.

All page redirect 301 with PHP code.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.