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
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.
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