Skip to main content

There are 2 types of redirects: 301 (moved permanently) and 302 (moved temporary).

You will need to set 301 response when:

  • moving to https
  • changing URLs structure
  • moving to another domain/subdomain

You can find settings for 301 redirect in Magento at Catalog > URL Rewrite

Press Add URL Rewrite. On the opened page choose Create URL Rewrite and specify Custom.

Fill the form that shown on the screenshot.

  • setting-301-redirect-magento-step-3-600x5541-1 How to setup a 301 redirect on your Magento siteType – the type of the redirect choice: Category, Product, Custom
  • Store – if you have more than 1 store, choose the one that needed
  • ID Path – add the relative reference to the target URL
  • Request Path – the path for the redirect
  • Target Path – the URL of the current page that should be a destination (ID path)
  • Redirect – set Permanent (301)
  • Description – add some text if needed

If you’re going to set redirects for the list of URLs, the described method can be too long. In this case, it will be better to use code. Add it to the root directory by path Magento/shell/rewriteurl.php

Check the list for duplicated URLs because if there are some it will cause errors during the code running.

Error example:

PHP Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Request Path for Specified Store already exists

Add the following code to the created file rewriteurl.php

<?php

require_once 'abstract.php';

class Mage_Shell_Rewriteurl extends Mage_Shell_Abstract
{
    public function run()
    {
    $old_url = [array old URL path];
    $new_url = [array new URL path];

        foreach ($old_url as $key => $value) {
            $rewrite = Mage::getModel('core/url_rewrite');
            /* @var $rewrite Mage_Core_Model_Url_Rewrite */
            $rewrite->setIsSystem(Mage::app()->getStore()->getId())
                ->setIdPath($old_url[$key])
                ->setRequestPath($old_url[$key])
                ->setTargetPath($new_url[$key])
                ->setOptions("RP")
                ->save();
        }
    }
}

$shell = new Mage_Shell_Rewriteurl();
$shell->run();

You’ll get the model from the variable $rewrite
setIsSystem(Mage::app()->getStore()->getId()) – specify store id
->setRequestPath($old_url[$key]) – previous URL
->setTargetPath($new_url[$key]) – new URL
->setOptions(“RP”) – RP for 301 redirect, R for 302 redirect

Open the terminal in the root Magento category, and run the code with the command:
php shell/rewriteurl.php

If the code executed without errors, you’ll see the row Success in the terminal.

Magento | Adobe Commerce SEO Guide:Sitemap and Robots.txt

seo_ico How to setup a 301 redirect on your Magento siteMobecls team provides basic and advanced SEO audits, SEO and performance optimization services. We are SEO-focused development company that specializes in Magento | Adobe Commerce. It means that we know what and how to fix SEO issues, taking into account your UX, Marketing and Business specifics. 

Vladimir Repalo

Vladimir Repalo

Magento Developer at Mobecls, 8+ years of experience. Feel free to ask me anything about this post in the comments below.