I want to redirect a custom cms page to home page
For example
to
I want to redirect a custom cms page to home page
For example
to
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();
}
}
A simple solution would be adding a URL rewrite from the Magento Admin Panel.
To do so:
- 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.
@Afzal Arshad
Hey,
you need to add below code in htaccess file
Redirect 302 /custom-cms-page https://magento246.com/
Thank You!
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.
Just need to add URL Rewrite
Requested URL : custom-cms-page
Targeted URL : https://magento246.com/
URL Rewrite : Permanent