1

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

10

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

<?php

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.

-2

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

All page redirect 301 with PHP code.

http://code.wewebtech.com/how-to-redirect-http-to-https-all-pages-using-htaccess/

1
  • 1
    Not related to Magento 2 Commented Feb 28, 2019 at 14:08

Your Answer

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

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