cancel
Showing results for 
Search instead for 
Did you mean: 

404 page url in magento 2

404 page url in magento 2

Hi All,

I am using Magento version 2.1.3 for my website.

We are getting the correct 404 page while accessing the non-existing url.
But i need to redirect the url to "/404" as well for the magento 404 page.

For Example:
I have accessed my website with "/abc" and getting 404 page but the url remains same as
www.domain.com/abc

I need to redirect the url to "/404" instead of getting the same url("/abc") what we have typed for every 404 Pages.

www.domain.com/abc should redirect to www.domain.com/404 if it is non existing url.

Please can any one suggest a best solution for this...

Thanks,
Shiva.

1 REPLY 1

Re: 404 page url in magento 2

Hi @soho_rize,

 

One way to perform such redirect is adding a plugin for \Magento\Cms\Controller\Noroute\Index. Configuration for etc/di.xml for this purpose:

 

    <type name="Magento\Cms\Controller\Noroute\Index">
        <plugin name="noroute_redirect" 
type="YourVendor\YourModule\Plugin\Noroute" /> </type>

And the plugin will look like:

<?php

namespace YourModule\YourVendor\Plugin;

class Noroute
{
    private $redirectFactory;

    public function __construct(\Magento\Framework\Controller\Result\RedirectFactory $redirectFactory)
    {
        $this->redirectFactory = $redirectFactory;
    }

    public function aroundExecute(
\Magento\Cms\Controller\Noroute\Index $index,
\Closure $proceed
) { return $this->redirectFactory->create()
->setPath('404notfound'); } }

You should also create a CMS Page specifying the 404notfound url-key to handle request to this url.

 

Unfortunately, Magento 2 framework does not allow to save pages with number-only url key.