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:
- Looking for Top web hosting? Clicking on this link can be the solution.