Magento category redirect cuts off the querystring

2020-05-03 11:17发布

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

3条回答
孤傲高冷的网名
2楼-- · 2020-05-03 11:58

In 1.9.1.0 this problem could be solved through patching in another class Mage_Core_Model_Url_Rewrite_Request/function _processRedirectOptions().
Just add after code

    $targetUrl = $this->_request->getBaseUrl() . '/' . $this->_rewrite->getTargetPath();

    $storeCode = $this->_app->getStore()->getCode();
    if (Mage::getStoreConfig('web/url/use_store') && !empty($storeCode)) {
        $targetUrl = $this->_request->getBaseUrl() . '/' . $storeCode . '/' . $this->_rewrite->getTargetPath();
    }

    if ($this->_rewrite->hasOption('R') || $isPermanentRedirectOption) {

the following

            $queryString = $this->_getQueryString();
            if ($queryString) {
                $targetUrl .= '?'.$queryString;
            }

and make sure 'if' statement keep closed with

           $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
    }

I'm sure it's fairly enough because of you don't need to transfer query string for external redirects.

Happy coding

查看更多
▲ chillily
3楼-- · 2020-05-03 12:02

I am running 2.1.0 community edition right now and am having the same issue. I tried finding the files above, but they seem to be specific to the 1.X versions of Magento (at least the implementation within the files). I've found a work around for this, but I hate hate hate the way I am doing it. That being said, I am not noticing any performance problems with the site and I didn't have to modify any Magento core files. So... here is what I did;

  1. I already had a directory under the Magento root directory that I use to host static content.

  2. I created two files in this directory: startscripts.js (which I use to load any custom scripts within the head element) and endscripts.js (which I use to load any custom scripts right before the end of the body element).

  3. In the administration page, go to content, configuration, and edit your site.

  4. In the html head section add

    <script src="/[staticDirectoryYouCreate]/startscripts.js" type="text/javascript"></script>

to the scripts and stylesheets section

  1. In the footer section on this same page, add

<script src="/[staticDirectoryYouCreate]/endscripts.js" type="text/javascript" defer></script>

to the Miscellaneous HTML section

  1. Here is the script that I created in the endscripts.js file to append the gclid to links on the page:

    try { var urlParameters = /gclid=([^&]+)/.exec(document.location.toString()); if(urlParameters) { if(urlParameters[1]) { var siteLinks = document.getElementsByTagName("a"); for(var currentLink=0;currentLink < siteLinks.length;currentLink++) { if(siteLinks[currentLink].href.toString().indexOf("gclid") < 0) { if(siteLinks[currentLink].href.toString().indexOf("?") < 0) { siteLinks[currentLink].href=siteLinks[currentLink].href+"?gclid="+urlParameters[1]; } else { siteLinks[currentLink].href=siteLinks[currentLink].href+"&gclid="+urlParameters[1]; } } } } } } catch(e){}

For this particular fix, you don't have to add the startscripts.js file, but the Google tag assistant was complaining that the Google Analytics snippet was outside of the head element, so I disabled the Magento Google Analytics implementation and put the snippet in the startscripts.js file.

I'll be curious to hear folks opinions on solving the problem this way.

查看更多
爷的心禁止访问
4楼-- · 2020-05-03 12:12

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

查看更多
登录 后发表回答