0

I have a custom module page url like this: www.example.com/style/index/content/id/19/

I want to convert this url like this: www.example.stylecontent.html with same content.

I have tried url rewrite but not getting success. can anyone help me to create this one?

I don't want to use admin url rewrite.

I am using this code:

ma

1 Answer 1

0

you can do this like that:

/**
* @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;
    parent::__construct(
        $context
    );
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.