【发布时间】:2017-02-03 17:53:49
【问题描述】:
我需要动态制作像“attribute1/attribute2/product-url-key”这样的产品网址。我发现每个产品的url都在enterprise_url_rewrite表,请求路径字段中。
我也想对索引器进程进行更改,以便在重新运行时保留更改,但我不知道它在哪里?
【问题讨论】:
标签: magento url-rewriting
我需要动态制作像“attribute1/attribute2/product-url-key”这样的产品网址。我发现每个产品的url都在enterprise_url_rewrite表,请求路径字段中。
我也想对索引器进程进行更改,以便在重新运行时保留更改,但我不知道它在哪里?
【问题讨论】:
标签: magento url-rewriting
下午好!让我们分解一下:
我需要动态制作像“attribute1/attribute2/product-url-key”这样的产品网址
是的 - 您可以使用代表您已经识别的数据库表的 Magento 模型动态创建 URL 重写:
/** @var Enterprise_UrlRewrite_Model_Redirect $rewrite */
$rewrite = Mage::getSingleton('enterprise_urlrewrite/redirect');
// Create new record or load the existing one
$rewrite->loadByRequestPath($requestUrl, $store->getId());
$rewrite
->setStoreId($store->getId()) // define which store the rewrite should be
->setOptions(null) // specify any rewrite/redirect/custom options
->setRequestPath($requestUrl) // specify the request URL
->setIdentifier($requestUrl)
->setTargetPath($targetPath) // specify the redirect target
->setEntityType(Mage_Core_Model_Url_Rewrite::TYPE_CUSTOM)
->setDescription('Add a comment if you want to');
$rewrite->save();
这将尝试通过$requestUrl 加载现有的 URL 重写/重定向,如果未找到它,将返回一个空模型,您可以用您的数据进行修饰并保存。
“选项”定义它是临时重定向还是永久重定向(302 与 301)。
More information here 通过 Magento EE 用户指南。
我也想对索引器进程进行更改,以便在重新运行时保留更改,但我不知道它在哪里?
别担心。 (现代)Magento 数据库在需要索引记录的地方都有表触发器,并将检测这些表上的创建、更新和删除。索引器将检测到需要进行的更改,并根据需要为您进行更改。
如果您看到 URL 重写消失,这很可能是因为您一直使用 SQL 将它们直接添加到索引表中,因此每当索引器运行时都会重写该表。为避免这种情况,请使用上述模型,所有内容都将保存到正确的位置并正确索引。
【讨论】: