Thursday , November 21 2024
Home > Magento Tips & News > Magento 2 Tutorials > Content > How to Create Magento 2 URL Redirect

How to Create Magento 2 URL Redirect

How to redirect url in Magento 2 - magento 301 redirect

When you run a Magento online store, there will be times you need to redirect an URL. It happens when you want to eliminate duplicate content or SEO-unfriendly slug. Knowing how to change to a different URL is essential in those cases. 

This blog post will show you how to create a Magento 2 URL redirect from an existing URL to a new one. Here are 2 ways for you to select. If you have little coding skills, use the default Magento 2 backend. Otherwise, you can create the redirect using code. 

Way 1: Create Magento 301 Redirect in the Backend

To change the destination URL of a Product page, category, or CMS page using Default Magento 2, check out our Magento 2 URL Rewrite here.

Learn more about redirect:
Magento 2: 301 Redirect Setup Techniques
301 Redirects For SEO: The Basic Know-How

Way 2: Create Magento 2 URL Redirect Programmatically

To create Magento 2 URL redirect programmatically, you can easily do via a controller like the one below: 

<?php
/**
 * Magezon
 *
 * This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to https://www.magezon.com for more information.
 *
 * @category  Magezon
 * @package   Magezon_Tutorial
 * @copyright Copyright (C) 2021 Magezon (https://www.magezon.com)
 */

namespace Magezon\Tutorial\Controller\Redirect;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
   
    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl('/'); // url you want to redirect
    }

}

That’s how to create a Magento 2 URL redirect. Have any questions? Comment down below to let us know. And don’t forget to visit our complete Magento 2 tutorial series, where we release new posts every week. 

Looking for fast, well-coded, yet affordable Magento 2 extensions? Then, visit the Magezon website.

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to build or optimize your Magento stores for sales-boosting? Then visit the Magezon website and grab the necessary add-ons for yours today!

About Alice Dinh

Alice Dinh
Alice is a writer at Magezon. She is on a mission to help readers learn about Magento and e-commerce to grow their online business. A chocolate fanatic once in a while.

Check Also

How to Create a Halloween Landing Page in Magento

It’s Halloween today. We’re gonna show you how to create a Halloween landing page using …

Leave a Reply