クエリ文字列を使用した URL からのリダイレクト
-
12-12-2019 - |
質問
私の古いサイトには、何万もの無ロンガーの製品がありました。 http://example.com?product_id=123456
.
これらはすべて検索エンジンでインデックス付けされており、Magento では 404 に解決されます。すべてのリダイレクトを作成することはできません。
私のホームページに解決してもらうにはどうすればよいですか?
解決
この場合、使用できます イベント/オブザーバー そしてイベントでは controller_front_init_before
監視員を解雇する。
まずそれだと思います 古い製品 ID と新しい製品 ID が同じです.
その上で observer check query string
($_SERVER['QUERY_STRING']) content
?product_id=製品ID パターン in url
それからこれの基本 redirect to current system product url
.
例:
仮定する、
旧システムの製品 URL: http://example.com?product_id=123456
新しいシステム製品の URL: http://example.com/abx.html
私のコードはリダイレクトされます http://example.com?product_id=123456
に http://example.com/abx.html
301リダイレクトを使用します。
301 リダイレクトを使用すると、SEO の問題が解決されます。
ここでの config.xml コードは次のとおりです。
<global>
<models>
<magento67130>
<class>StackExchange_Magento67130_Model</class>
<resourceModel>magento67130_mysql4</resourceModel>
</magento67130>
</models>
<events>
<controller_front_init_routers> <!-- identifier of the event we want to catch -->
<observers>
<controller_front_init_before_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>magento67130/observer</class> <!-- observers class alias -->
<method>redirectionProduct</method> <!-- observer's method to be called -->
</controller_front_init_before_handler>
</observers>
</controller_front_init_routers>
</events>
</global>
Observer.php コードは以下のとおりです。
<?php
class StackExchange_Magento67130_Model_Observer
{
public function redirectionProduct(Varien_Event_Observer $observer)
{
return;
if($this->_getQueryString()){
print_r($this->_getQueryString());
// Check query string exits in product_id
if(strpos($this->_getQueryString(),'product_id=')!== false){
/* get Product ifd from query string */
$productid=str_replace('product_id=','',$this->_getQueryString());
if(!empty($productid)):
$product= Mage::getModel('catalog/product')->load($productid);
if ($product->getId()) {
echo $product->getProductUrl();
Mage::app()->getResponse()
->setRedirect($product->getProductUrl(), 301)
->sendResponse();
return;
}
endif;
}
}
//die();
}
/* Check here Request query string */
protected function _getQueryString()
{
if (!empty($_SERVER['QUERY_STRING'])) {
$queryParams = array();
parse_str($_SERVER['QUERY_STRING'], $queryParams);
$hasChanges = false;
foreach ($queryParams as $key => $value) {
if (substr($key, 0, 3) === '___') {
unset($queryParams[$key]);
$hasChanges = true;
}
}
if ($hasChanges) {
return http_build_query($queryParams);
} else {
return $_SERVER['QUERY_STRING'];
}
}
return false;
}
}
他のヒント
you have to create one little custom extension to redirect if product is not found in your database
here i have given working solution and its perfect worked for me
How to forward all the 404 Magento pages to the front page?
hope this will also work for you.