Magento: remove trailing slash from the website URL
A note on how to remove the slash at the end of the URL without damaging the store.
What it’s for: For a server, mydomain.com/category
and mydomain.com/category/
are the same. But for search engines these are two different pages with the same content. And this is important for SEO.
What do we have to do:
First we need to slightly fix the getUrl()
function so that the generated URLs do not have a trailing slash. In order not to make changes to the core itself (which is fraught with the loss of all custom functionality when updating Magento), copy the file app/code/core/Mage/Core/Block/Abstract.php
to app/code/local/Mage/Core/Block/Abstract.php
.
Find the getUrl()
function in the app/code/local/Mage/Core/Block/Abstract.php
file (line 941):
public function getUrl($route = '', $params = array())
{
return $this->_getUrlModel()->getUrl($route, $params);
}
Change the code of this function to this one:
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 substr($return_url, 0, -1);
else:
return $return_url;
endif;
}
Next, we will need to edit .htaccess
in the root of the site with the following lines:
RewriteCond %{request_method} ^GET$
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)$ %1 [L,R=301]
In the standard .htaccess
this should be added after the line:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
If you have a multi-store, then the rules in .htaccess
must be added for each store.