Shopping cart

Magento 2 Add URL Rewrite programmatically

Magento 2 URL Rewrite programmatically is one of the awesome solutions for online retailers as you who want to really create a huge number of the traffics of your website. The purpose of rewriting the url is allowing you generating 301 redirects into Magento 2 by the programmatical way.

What is URL Redirect?

URL redirect is a popular term in SEO and it is used for navigating the visitors to any link store owners expect. There are two main type of redirect: 301 redirect and 302 redirect. Therefore, if you are wondering how to continue working with the existing visitor at the current site while you are planning to build a new site with more efficiencies, the best answer is creating the search redirection through the redirect 301.

Overview of Magento 2 URL Rewrite programmatically

Step 1 : Generate constructor file

* @var \Magento\UrlRewrite\Model\UrlRewriteFactory
protected $_urlRewriteFactory;

* @param Context $context
* @param \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
public function __construct(
    Context $context,
    \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
) {
    $this->_urlRewriteFactory = $urlRewriteFactory;

Step 2 : Insert custom URL rewrite in execute method

Only if your original website is example. com/customModule/customController/customAction, you want to rewrite the URL by www.example. com/xyz, instead. Then you can run the following method:

$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */
/* this url is not created by system so set as 0 */
/* unique identifier - set random unique value to id path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
/* set requested path which you want to create */
/* set current store id */

Related Post

Enjoyed the tutorial? Spread it to your friends!


Sam Thomas
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

Comments for Magento 2 Add URL Rewrite programmatically

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

People also searched for

  • magento 2 url rewrite programmatically
  • Magento 2 URL Rewrite
  • magento 2 url rewrite management
  • magento 2 url rewrite id path
  • magento 2 url rewrite not working
  • magento 2 url rewrite to homepage
  • magento 2 url rewrite import
  • magento 2 url rewrite category to cms page
  • magento 2 url rewrite extension
  • magento 2 url rewrite table
  • magento 2 url rewrite custom module
  • magento 2 url rewrite seo
  • magento 2 url rewrite target path
  • magento 2 url rewrite tutorial
  • magento 2 update core url rewrite
  • magento 2 url rewrite video
  • magento 2 url rewrite verwaltung
  • magento 2 url rewrite with parameters
  • magento 2 url rewrite windows
  • magento 2 url rewrite www
  • magento 2 redirect url with parameters
  • magento 2 url rewrite xml
  • magento 2 reindex your url rewrite index
  • magento 2 url redirect 301
  • magento 2 url rewrite 404
  • magento 2 add url rewrite for a category
  • 2.1.x, 2.2.x, 2.3.x