Read More
Read More
Menu Close

    Magento2 Programmatically Create Custom URL Rewrites

    Magento2 Programmatically Create Custom URL Rewrites – For normal users there is a common need of custom URLs for controlling their websites and they can create their custom URLs using magento2
    URL rewrite features, but if you want to create custom URLs programmatically then the solution is here. Here I am going to explain the process to create custom URL using code in controller file.

    Step 1 : Create constructor file.

    /**
    * @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory
    */
    protected $_urlRewriteFactory;
    
    /**
    * @param Context $context
    * @param \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
    */
    public function __construct(
    	Context $context,
    	\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
    ) {
    	$this->_urlRewriteFactory = $urlRewriteFactory;
    	parent::__construct(
    		$context
    	);
    }

    Step 2 : Create custom URL rewrite in execute method.

    Like your website actual URL is www.example.com/customModule/customController/customAction but you want to execute this URL on click on www.example.com/xyz (requested URL) then you can create by following method-

    $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();

    So in this way you can create custom URL. Thanks!

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    8 comments

  • E.J. Sexton
    There are some issues with the code:

    1. `$this->_eavAttributeFactory = $eavAttributeFactory;` should be replaced with `$this->_urlRewriteFactory = $urlRewriteFactory;`
    2. Should use `\\Magento\\UrlRewrite\\Model\\UrlRewriteFactory` instead of `\\Magento\\UrlRewrite\\Model\\ResourceModel\\UrlRewriteFactory`.

    • ashutosh srivastava (Moderator)
      This article is very old, we have updated the article now.
      Thanks
  • SAndeep
    its not working. error: Exception #0 (Exception): Notice: Undefined variable: eavAttributeFactory

    i tried this:

    public function __construct(
    MagentoBackendAppActionContext $context,
    MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
    )
    {
    $this->_urlRewriteFactory = $urlRewriteFactory;
    parent::__construct($context);
    }

    and under execure()

    if(isset($data[‘url_key’])){
    $urlRewriteModel = $this->_urlRewriteFactory->create();
    $urlRewriteModel->setStoreId(1);
    $urlRewriteModel->setIsSystem(0);
    $urlRewriteModel->setIdPath(rand(1, 100000));
    $urlRewriteModel->setTargetPath(“www.example.com/customModul…”);
    $urlRewriteModel->setRequestPath(“www.example.com/xyz”);
    $urlRewriteModel->save();
    }

    error coming:

    Fatal error: Call to undefined method MagentoUrlRewriteModelResourceModelUrlRewrite::setStoreId()

    i am using this in Save.php when user try to change / edit content of grid.

  • Rafael Corrêa Gomes ♛
    Thanks for sharing!
  • satish.patel438
    can you explain why do i need to define “$this->_eavAttributeFactory” in constructor??
    I have tried same thing but it is not working for me.
  • zarana Patel
    Where we need to put step2 code?
    • satish.patel438
      You Can put step2 code in execute method.
      public function execute()
      {
      }
      • SAndeep
        i tried this:

        public function __construct(
        MagentoBackendAppActionContext $context,
        MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
        )
        {
        $this->_urlRewriteFactory = $urlRewriteFactory;
        parent::__construct($context);
        }

        and under execure()

        if(isset($data[‘url_key’])){
        $urlRewriteModel = $this->_urlRewriteFactory->create();
        $urlRewriteModel->setStoreId(1);
        $urlRewriteModel->setIsSystem(0);
        $urlRewriteModel->setIdPath(rand(1, 100000));
        $urlRewriteModel->setTargetPath(“www.example.com/customModule/customController/customAction”);
        $urlRewriteModel->setRequestPath(“www.example.com/xyz”);
        $urlRewriteModel->save();
        }

        error coming:

        Fatal error: Call to undefined method MagentoUrlRewriteModelResourceModelUrlRewrite::setStoreId()

  • Back to Top