How To setup URL Rewrites In Magento 2

How To setup URL Rewrites In Magento 2

Introduction

SEO is a phenomenal online marketing strategy that enables Magento store owners to create fast, user-friendly, and robust websites that rank higher in search engines. Ranking high in search results brings more qualified customers to your online store and eventually leads to increased conversion rates. For this reason, setting up the URL rewrites on your Magento store is momentous to the success of your online marketing strategy.

URL rewrites provide an expedient and secure way of making the existing URLs SEO-friendly and easier to read by humans. Magento is engineered to routinely utilize URL rewrites to eliminate the file value “index.php” that features in existing URLs just after the root folder. When the Web Server Apache Rewrites are activated any word that adds no value to SEO is removed.

This tutorial will help you set up URL rewrites in Magento 2 to aid your website performance, improve search engine ranking, and enhance the readability of your text.

Ready? Let’s get started!

Step 1:
Enabling URL Rewrites

To enable URL rewrites in Magento 2, log in to your store’s back-end. Click Stores, select Configuration and expand the Web section.

Click the Arrow on the Search Engine Optimization to open the drop-down menu. Locate the Use Web Server Rewrites option.

Set this option to YES.

Next, click Save Config for the changes to take effect.

Step 2:
Setting Up Automatic URL Redirects

Configuring automatic URL redirects allows your Magento 2 store to automatically initiate permanent redirect every time the URL key of any product is changed. To successfully setup automatic URL redirects in Magento 2 do the following.

Login to your Magento 2 Admin Area. Click Stores, select Configuration and expand the Catalog section.

From the drop-down options, select Search Engine Optimization and set Create Permanent Redirect for URL if URL Key is Changed to YES.

Click Save Config to implement the changes.

Step 3:
Using Codes To Manage URL Rewrites in Magento 2.

Another effective way of managing URL redirects is activating the 301 redirect. To accomplish this, follow the steps below:

  • Generating the constructor file using the command below:

/**
* @var MagentoUrlRewriteModelResourceModelUrlRewriteFactory
*/protected $_urlRewriteFactory;
/**
* @param Context $context
* @param MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
*/publicfunction__construct(
    Context $context,
    MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
) {
    $this->_eavAttributeFactory = $eavAttributeFactory;
    parent::__construct(
        $context
    );
}
  • Creating A Custom URL Rewrite In The Execute Method

If your actual URL is www.example.com/customModule/customController/customAction, then you can create URL redirect on a requested URL such as www.example.com/xyz. This can be achieved using the method below:

$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */$urlRewriteModel->setStoreId(1);
/* this url is not created by system so set as 0 */$urlRewriteModel->setIsSystem(0);
/* unique identifier - set random unique value to id path */$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
/* set requested path which you want to create */$urlRewriteModel->setRequestPath("www.example.com/xyz");
/* set current store id */$urlRewriteModel->save();

Congratulations! You have successfully created custom URL programmatically.

Conclusion

That is it! You have successfully configured URL redirects in your Magento 2 store. By setting up the URL redirects, it becomes easy to redirect your store visitors from the old catalog pages to the newly created products. Besides, this will boost your visibility online by enabling you to use high-value keywords that offer unprecedented support for product index in search engine optimization.

Check out the top 3 Magento hosting services:

HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8
Hostinger
$1.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7

How To Submit Your Website to the Google Search Console

Starting a website has become so easy with the content management systems li
4 min read
Angela Olaru
Angela Olaru
Senior Writer & Hosting Expert

How To Prevent Extreme SEO Mistakes in Magento

This tutorial will help you eliminate SEO mistakes before launching your new onl
3 min read
sarah
sarah
Hosting Expert

How to Disavow Bad Backlinks From an External Page Using cPanel

SEO plays a vital role and you should always keep an eye on backlinks. This how-
3 min read
Arvind Singh
Arvind Singh
Hosting Expert

How to Boost the SEO of your website using these 10 simple to follow methods

Once in a while, we all come across articles related to SEO and how
4 min read
Arvind Singh
Arvind Singh
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top