I have custom frontend controller, I added the URL Rewrite for this controller

I want to figure out the way, How I send requesting URL to 404 page based on some condition within the controller.

I don't want the noroute redirect solution because it change the URL. For example if you disable the product, URL remain same but it goes to 404 page and also I don't want to remove the URL Rewrite for this controller as well.

Any help, experience and knowledge sharing would be appreciated.


Not sure if it is valid according to best practices but following also works for me:

use Magento\Framework\Exception\NotFoundException;


public function execute()
    if (something) {
        throw new NotFoundException(__('Page Not Found'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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