How to Create URL Redirect in Magento?

Jul 9, 2019836
How to Create URL Redirect in Magento?

Why do you need redirect in your ecommerce business? What types of redirect do you know? What are their benefits and opportunities for your website? In this article, we will describe Magento 301 and 302 redirects, consider their main differences and various pros and cons. Find out how to create a Magento 301 redirects from admin panel and how to perform a 301 redirect in Magento programmatically.

In ecommerce business, sometimes you need to change or rewrite your URL for several reasons. For instance, in the case of duplicate content or not user-friendly URL. A good solution to improve your website and advance SEO is URL relocation.
Redirect is a common practice in Magento ecommerce that helps improve customer experience and maintain the brand’s authority.

Magento URL redirect can be used to create a product, category and custom relocations for any page in your webstore. As a result, any links to the previous URL are moved to the new address. A custom redirect allows to relocate CMS pages, or any other pages. You are able to use Magento redirects to create additional URLs for items, both temporarily and permanently.

Let us consider what URL redirects actually are, their types, pros and cons.

Table to contents:

What is a URL redirect?
What are the types of redirect?
301 vs 302 redirect – differences, advantages and disadvantages
When and why should you use a Magento redirect?
How to create a 301 and 302 redirect in Magento in admin panel?
How to programmatically create 301 redirect in Magento 2?
How to create Magento URL redirect from controller?

What is a URL redirect?

If you want to remove pages from your website, change your URLs or domain name, you need redirect. Otherwise, links to your site will be useless. So, it’s crucial to choose the correct redirect type. A web server function that redirects a customer from one URL to another is called a URL redirect, also known as HTTP code 3xx redirect, URL forwarding, domain redirection and forwarding.

There are different types of redirects: HTTP 3xx series status codes (the most popular), manual redirects, server-side scripts, JavaScript, frame redirects and metatag refreshes.

Why customers can be redirected? The main reasons are:

  • A business name was changed
  • Two websites were united
  • To direct traffic to renovated content
  • To direct content to a recently updated domain name
  • Landing page-split testing for marketing tests

When a customer is relocated to a newly named domain, the website address receives a new URL. Companies often change their original home page into a redirect one with a message briefly describing it. A meta refresh tag is inserted into the website’s source code. Without a relocation, clients would get a “404 – Not Found” error message.

Redirect can be performed via a redirection service that operates through software. Considering the meaning of URL redirect, let us describe its main types of redirects.

magento integration

Magento 2 Integration

Take your online store to the next level with BelVG Magento integration

Visit the page

What are the types of redirect?

It is possible to use redirects to create additional URLs for items, as a temporary or a permanent change.
The most popular redirect types are:

  1. 300, “Multiple Choices”
  2. 301, “Moved Permanently”
  3. 302, “Found” or “Moved Temporarily”
  4. 303, “See other”
  5. 305, “Use proxy”

300 (Multiple Choices)

Designed for automatic (sometimes manual) browser selection of the most suitable for the user request document. Most commonly, 300 redirect is automatic, so all comes down to the browser choosing a document with the needed language and / or encoding.

301 (Moved Permanently)

Magento 301 redirect is a permanent type referring to the HTTP. Being the most commonly used and powerful, this Magento redirect passes on almost all of the link juice of the existing domain. It occurs on a browser and server level. Typing a URL into your browser or clicking on a link sends an inquiry for the page to the site server. A Magento 301 redirect include instructions performed when the request hits the server, automatically moving to the other page. It is the center of most SEO efforts during a site migration.

302 (Found (HTTP 1.1) / Moved Temporarily (HTTP 1.0)

Magento 302 redirect is used for a temporal URL relocation if you want to move back to the old URL. For example, while redesigning your site. Magento 302 redirects are rarely used and less popular than Magento 301 ones.

303, (See other)

This specific redirect is used in the situation when search results consist of a single link. In order not to waste the user’s time by displaying a page with a single search result, the system immediately redirects it them to the destination page.

305, (Use proxy)

This type of redirect indicates that access to the requested document can only be obtained through a proxy server.
It is clear from all the redirects’ description that 301 and 302 must be the most commonly used ones in ecommerce, and it is indeed so. Therefore, we will concentrate further on these two types and how to manage 301 and 302 redirects in Magento.

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad

301 vs 302 redirect – differences, advantages and disadvantages

When clients are switched to another page, they don’t think about the type of redirect. Search engines are responsible for this. Magento 301 and 302 redirects have some differences: 

  1. Magento 301 redirect is permanent, and 302 one is temporary.
  2. Magento 301 redirect is useful for SEO. It passes the link juice from one page to another. As a result, you don’t lose traffic and rankings.
  3. Magento 302 redirect may not pass all link juice and may be harmful for SEO in the future due to its temporary nature.

Let us consider Magento 2 redirects. 

The most typical examples of Magento 302 redirect are:

  1. Add to compare links
  2. Add to wishlist links
  3. Geolocation and currency switch links

Solution for 302 redirects in compare, wishlist, geolocation/currency switch links in Magento

  1. Keep the 302 redirects
  2. Disallow indexing of these URLs in your robots.txt file 
  3. Solve more serious issues 

Wrong Magento 301 redirect usage could cause serious problems with browsers used by customers in your store.

Considering the main differences between Magento 301 and 302, let us describe their advantages and disadvantages. 

Magento 301 redirect

Advantages 

  1. Magento 301 redirect is better for SEO than a temporary type as it moves the inbound links from the redirected domain to the new one.  Thus, it helps the website maintain its search rankings and prevent search traffic decline. 
  2. This type is more preferable for customers and search engines. It shows that the page has moved permanently. The engines will move any link from the original page to the new URL. 

Disadvantages 

  1. Moving a page from one URL to another, the search engines need much time to discover and recognize the Magento 301 redirect, and credit the new page with the rankings. 
  2. Transferring content becomes more complicated if a website changes its domain or when content moves from one domain to another. Due to spammers attacks and search engines suspicion, 301 redirects need a great amount of time to be correctly examined. 

Magento 302 redirect

Advantages:

  1. Magento 302 redirect is a good option if a site admin deliberately doesn’t want to pass link equity from the old page to the new one.
  2. This type would be preferable when website content needs to be moved temporarily, like in the case of a site maintenance. Clients need to be redirected to another domain to use the content.

Disadvantages:

  1. Magento 302 redirect doesn’t provide the rankings and search engine rates like a 301 type. 
  2. This redirect may not pass any link juice, which may undermine the quality of SEO.
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad

When and why should you use a Magento redirect?

Magento redirects are necessary if you don’t want to lose essential website links. It helps your customers not to see a Magento redirect 404 to homepage. 

What are the main reasons for using Magento redirect? 

  1. You have duplicate content

When there are a lot of versions of the same content,  it’s rather complicated for Google to select the right page. To solve this problem, use a Magento 301 redirect on the duplicate content to direct to the original page. As a result, you will advance your search engine rankings and improve customer experience. 

  1. You’ve changed your domain

Changing your domain name, you don’t want to lose any useful links. You should install a permanent redirect from your old domain to a new one. In this case, the best practice is to perform a Magento redirect to homepage.

  1. You have multiple domains

Some retailers can buy multiple domains to protect their brand. In this case, they will redirect any of other domains to a new one. It allows to get extra traffic and prevent opponents from buying a similar domain which can be redirected to their site.

Magento redirects are profitable and beneficial for retailers helping them maintain a good SEO strategy and improve customer experience. 

Why do you need a Magento 301 Redirect?

  1. To associate universal web conventions (http://, www., etc.) with one URL to enlarge domain authority 
  2. To rebrand or rename a site with a different URL
  3. To direct traffic to a website from other URLs owned by the same organization

If a brand is changing its company or website name, a Magento 301 redirect is essential to transfer inbound links of the original URL on the new domain. Moreover, it is needed to redirect clients to the right web address.

Brands sometimes can buy domains similar in name to their brand to increase search traffic to their website. A Magento 301 redirect is necessary to ensure that the brand’s original domain maintains its search power. 

Magento store.belvg.com

Magento 2 Extensions

Take you online store to the next level with Magento 2 extensions by BelVG

Visit the store

How to create a 301 and 302 redirect in Magento in admin panel?

Why should you know how to rewrite URLs? There are some reasons why it’s really beneficial:

  • If you decide to move your website to another ecommerce platform
  • If you don’t want to offer products from some categories
  • If you need to change your URL to make it more precise 

Both 301 and 302 redirects can be created and set up from Magento back office by a store admin. Below, you will find a concise step-by-step instruction, that demonstrates how to perform this.

Step #1: log in to Magento admin panel and navigate to Marketing -> SEO & Search -> URL Rewrites. 

marketing seo url rewrites

Step #2: click the Add URL Rewrite button in the upper-right part of the page. 

add new rewrite

Step #3: select the type of Magento URL rewrite in Create URL Rewrite field: custom, for category, product or CMS page.

Depending on your choice, the further configuration will be different; we will go over all four types of URL rewrites.

Step #4: create a custom URL rewrite.

custom url rewtite

Fill in the following fields:

  • Store – if you have multiple stores, indicate where exactly your changes will be 
  • ID Path – the name of your rewrite, used for admin goals
  • Request path – type your old URL
  • Targeted path – it is where your new URL must be
  • Redirect – select the redirect type: permanent or temporary
  • Description – type your new URL description 

Press the Save button in the top right corner of the page to save the changes.

Step #5: create a category URL rewrite.

Select For Category option in Create URL Rewrite drop down. 

Select the category in the appeared Add URL Rewrite for a Category menu.

url rewrite for category

Complete the following fields to create the Magento URL rewrite for the category. 

add url rewrite for a category

Press Save when finished.

The course of action will be similar in the two left cases – URL rewrites for CMS and product pages, only instead of Add URL Rewrite for a Category you will be offered a list of CMS or product pages to redirect from.

All created links are stored in the database in the url_rewrite table. If you view this table, you will see an interesting column: is_autogenerated. The matter is that some links are automatically generated by the system. For example, you save CMS pages, Magento provided the Observer for the cms_page_save_after” event. If a page was edited or saved and its URL Key or Store_id has changed, then the system will automatically add or change the redirect in the url_rewrite table. The type will be set to “No” and the is_autogenerated flag will be 1. For example, if the URL Key changes to a category, the system will automatically rewrite all URLs for the category, child categories and child products of this category, and will also make a 301 redirect from the old link to the new one, so that the search robots would correctly process the page transfer.

Creating this redirect from an old link to a new one is regulated by the option “Create Permanent Redirect for URLs if URL Key Changed”, by Admin -> Stores -> Configuration -> Catalog -> Catalog -> Search Engine Optimization

So, following this instruction, you won’t have any problems creating Magento 301 and 302 redirects. As a result, all your customers will be redirected to the correct web address, that will improve their shopping experience.

magento integration

Magento 2 Integration

Take your online store to the next level with BelVG Magento integration

Visit the page

How to programmatically create 301 redirect in Magento 2?

Below is the easiest way of how to do a 301 redirect in Magento programmatically:

This redirect class (Magento\Framework\Controller\Result\Redirect) also includes such methods as:

  • setRefererUrl – sets redirect URL from a referral link (HTTP_REFERER) 
  • setRefererOrBaseUrl – sets a link from the first point or uses a base URL
  • setUrl – sets a link for a manual redirect 
  • setPath – with it you can set a path and Magento 2 will redirect to a full URL 

How to create Magento URL redirect from controller?

In addition to the programmatic method described above, we can create a console command, which will set up a redirect with certain parameters. 

Follow the link to view the module source code at GitHub https://github.com/mavzu/belvg-rewrite.

Generally, a console command looks the following way:

belvg:redirect:create <old_url> <new_url> <redirect_code>

Where:

  • old_url – Requested URL
  • new_url- Target URL
  • redirect_code – code which redirects us on the other page

Alternatively, you are able to use .htaccess file for creating redirect .In the field <IfModule mod_rewrite.c> … </IfModule> , insert a redirect code, for example:

As a result, you will create Magento 301 redirect  to another website with parameters passage. 

Magento .htaccess redirect is also valid, for example, in the case of redirect Magento 2 non-www to www. The solution to this problem is the following:

Wrapping it up

So, we hope this article was rather informative and helpful. You have learned about Magento redirect types, their pros and cons for your ecommerce business. In addition, you discovered how to create Magento 301 and 302 redirects from admin panel without any difficulty. Moreover, now you have enough experience to perform a Magento 301 redirect programmatically. We believe it will help you maintain a good SEO strategy, and avoid losing search traffic and rankings. In addition, you will maintain all important website links. Finally, you will be able to improve customer experience and maintain your brand authority. 

Did you create Magento 301 or 302 redirect? Was it a beneficial practice for your webstore or did you experience challenges with them? Did you improve the customer experience? Share your comments with us. 

If you have any questions, feel free to ask in the comments.

magento development services

Magento Development Services

Take your online store to the next level with BelVG Magento development

Visit the page

How useful was this post?

Average rating / 5. Vote count:

Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Vlad

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *