0

I want to redirect a custom cms page to home page

For example

https://magento246.com/custom-cms-page

to

https://magento246.com/

5 Answers 5

1

Here is code for redirect from custom cms page to Homepage

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Cms\Controller\Page\View">
        <plugin name="yourmodule_view_page_plugin" type="Vendor\Module\Plugin\ViewPlugin" sortOrder="1" disabled="false"/>
    </type>
</config>

app/code/Vendor/Module/Plugin/ViewPlugin.php

<?php

namespace Vendor\Module\Plugin;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;

class ViewPlugin
{
    /**
     * @var RequestInterface
     */
    private $request;

    /**
     * @var ResultFactory
     */
    private $resultFactory;

    /**
     * Constructor
     *
     * @param RequestInterface $request
     * @param ResultFactory $resultFactory
     */
    public function __construct(
        RequestInterface $request,
        ResultFactory $resultFactory,
        UrlInterface $url
    ) {
        $this->request = $request;
        $this->resultFactory = $resultFactory;
        $this->url = $url;
    }

    /**
     * Plugin method to intercept execute() method of Magento\Cms\Controller\Page\View
     *
     * @param \Magento\Cms\Controller\Page\View $subject
     * @return \Magento\Framework\Controller\ResultInterface|null
     */
    public function aroundExecute(
        \Magento\Cms\Controller\Page\View $subject,
        callable $proceed
    ) {
        $pageId = $this->request->getParam('page_id') ?? $this->request->getParam('id');
        
        $customPageId = 21 ;// Your Custom Page Id 

        // Example: Redirect to homepage if page ID is 21
        if ($pageId == $customPageId) {

            $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($this->url->getUrl('')); // Redirect to homepage
            return $resultRedirect;
        }
        return $proceed();
    }
}
1
  • Please accept this as solution Commented Jun 27 at 11:28
1

A simple solution would be adding a URL rewrite from the Magento Admin Panel.

To do so:

  • Login to Magento Admin Panel and go to Marketing > SEO & Search > URL Rewrites
  • Remove rewrite for request path = custom-cms-page if already exists
  • Add a custom URL rewrite with the following details:
  • Create URL Rewrite: Custom
  • Request Path: custom-cms-page
  • Target Path: /
  • Redirect Type: Permanent (301)

and Save the URL Rewrite.

Flush Magento Cache and check front end of the website.

1

@Afzal Arshad

Hey,

you need to add below code in htaccess file

Redirect 302 /custom-cms-page https://magento246.com/

Thank You!

0

window.location.href = "https://magento246.com/"

There are multiple ways. One way is using Javascript code to do redirect. You can add this script in admin cms page.

0

Just need to add URL Rewrite

Requested URL : custom-cms-page

Targeted URL : https://magento246.com/

URL Rewrite : Permanent

enter image description here

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.