itreniets itreniets - 5 months ago 74
PHP Question

Magento category redirect cuts off the querystring of an url

we are running a Magento 1.4.2.0 Webshop with google analytics.
As we all know google attaches a querystring called the "gclid-Param" to the url.

The customer clicks the following url: http://www.myshop.com/bathrooms/showersbaths.html?glicd=somevalue

The category "bathrooms" was renamed inside magento, so magento automatically created a redirect from the old categoryname to the new name "bathroom furniture".

So now we have the problem, that magento cuts off the querystring with the glic-param when it rewrites and redirects the url.

Does anybody know how to prevent this or in which core-Module we have to modify the building of the new url?

best regards
Markus

Answer

After some more deep research inside the chaos of magento we found the solution to solve our Problem.

In the Url-Model of the Mage_Core exists a class rewrite.php. We created a custom model and overwrited the rewrite.php.

Inside of the function rewrite(), we added the following piece(marked as comments) of code:

//$url_params = false;
//if ($url_params = $_SERVER['QUERY_STRING'])
//$url_params = "?".$url_params;

if ($external === 'http:/' || $external === 'https:') 
{
    if ($isPermanentRedirectOption) 
    {
        header('HTTP/1.1 301 Moved Permanently');
    }

    header("Location: ".$this->getTargetPath() //.$url_params);
    exit;
} 
else 
{
    $targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
}
$isRedirectOption = $this->hasOption('R');
if ($isRedirectOption || $isPermanentRedirectOption)
{
    if (Mage::getStoreConfig('web/url/use_store') && $storeCode =    
    Mage::app()->getStore()->getCode()) 
    {
        $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/'  
        .$this->getTargetPath();
    }
    if ($isPermanentRedirectOption)
    {
         header('HTTP/1.1 301 Moved Permanently');
    }
    header('Location: '.$targetUrl //.$url_params);
    exit;
}

So i hope our solution helps others, who are facing the same problem.

Best regards Markus