Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2以编程方式添加URL重写

Magento 2 URL重写以编程方式是在线零售商的绝佳解决方案之一,因为您希望真正创建大量网站流量。重写URL的目的是允许您以编程方式生成301重定向到 Magento 2

什么是网址重定向?

URL重定向SEO中的流行术语,用于将访问者导航到商店所有者期望的任何链接。重定向主要有两种类型:301 redirect和302 redirect。因此,如果您想知道如何在计划以更高的效率构建新站点时如何继续与当前站点上的现有访问者合作,最好的答案是通过重定向301创建搜索重定向。

Magento 2 URL编程概述

步骤1:生成构造函数文件

/**
* @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
    );
}

第2步:在execute方法中插入自定义URL重写

只有当你的原始网站是example. com/customModule/customController/customAction,你要重写URL通过www.example. com/xyz,而不是。然后,您可以运行以下方法:

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

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论