Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active February 2, 2021 19:11
Show Gist options
  • Save evgv/e7e84e84c46e027c3fff7396a13b3a43 to your computer and use it in GitHub Desktop.
Save evgv/e7e84e84c46e027c3fff7396a13b3a43 to your computer and use it in GitHub Desktop.
Magento. Remove Trailing Slash From Magento URLs

Remove Trailing Slash From Magento URLs

Duplicate Content Issue

from http://www.example.com/category.html/ to http://www.example.com/category.html

Step 1

Go to ../app/code/core/Mage/Core/Block/Abstract.php copy this file to ../app/code/local/Mage/Core/Block/Abstract.php

Find the following line of code:

    return $this->_getUrlModel()->getUrl($route, $params);
    

and replace method getUrl() with following instructions

before
    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = array())
    {
        return $this->_getUrlModel()->getUrl($route, $params);
    }
after
      /**
     * Generate url by route and parameters
     *
     * Rewrited method for delete traiking slash 
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = array())
    {
        $return_url = $this->_getUrlModel()->getUrl($route, $params);
 
        if (
            $return_url != $this->getBaseUrl() && 
            substr($return_url, -1) == '/' && 
            !Mage::getSingleton('admin/session')->isLoggedIn()
            ) {
 
            $return_url = substr($return_url, 0, -1);
        }
 
        return $return_url;
    }

Step 2

Go to ..app/code/core/Mage/Core/Model/Url.php copy this file to ..app/code/local/Mage/Core/Model/Url.php

Find the following line of code:

  if ($noSid !== true) {
 
      $this->_prepareSessionUrl($url);
 
  }

and replace method php ($noSid !== true) to php ($noSid == false) to make sure that our variable $noSid is exactly equal false, result must be like following code examples

before
    if ($noSid !== true) {
        $this->_prepareSessionUrl($url);
    }
after
    if ($noSid == false) {
        $this->_prepareSessionUrl($url);
    }

Step 3 (optional)

Only if you need redirect from pages with trailing slash to pages without

Edit your .htaccess file if it exist(used in Apache server)

Find the following line of code:

    RewriteRule .* – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Add the following immediately underneath it:

    RewriteCond %{request_method} ^GET$
    RewriteCond %{REQUEST_URI} !^/downloader.*$
    RewriteCond %{REQUEST_URI} ^(.+)/$
    RewriteRule ^(.+)$ %1 [L,R=301]
@evgv
Copy link
Author

evgv commented Nov 25, 2019

Thanks so much for this! Two questions:

  1. This already includes a 301 redirect for the existing URLs and addresses future one, right?
  2. Would a similar fix apply to the product review pages Magento creates? Basically, if the URL of a PDP was website.com/product123 , the system creates a duplicate under website.com/product123/reviews
  1. Yep, this is work on fly for all URLs.
  2. If I right understood, for duplicates you need another solution like a rewrite with 301 or 302 code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment