Adding trailing slashes to Magento for SEO purposes

Mon, 06/20/2011 - 14:38

Submitted by markoshust Mon, 06/20/2011 - 14:38

By default, Magento does not automatically add on trailing url's to the end of every URL. In a database-driven cms system, you will be penalized for this by having two urls' with duplicate content: http://yourinstall.com/home http://yourinstall.com/home/ In the case above, each of these url's will lead to the same content, causing search engines to spider both pages, and in turn penalizing your website for having duplicate content. You can either create a 404 error for one of the pages, or redirect everything to a standard url. Adding trailing slashes seems to have been the standard for quite a while now and is how most cms systems manage their url's. Below is a diff you can use on the .htaccess file in the root of your Magento install.

diff --git a/.htaccess b/.htaccess
index 9acb08b..b8daffd 100644
--- a/.htaccess
+++ b/.htaccess
@@ -123,6 +123,13 @@
     #RewriteBase /magento/
 
 ############################################
+## add trailing slashes to url's if they don't exist
+
+    RewriteCond %{REQUEST_FILENAME} !-f
+    RewriteCond %{REQUEST_URI} !(.*)/$
+    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
+
+############################################
 ## workaround for HTTP authorization
 ## in CGI environment

Comments

When I add the above statement to my htaccess, it also adds the trailing slash to product and category pages: e.g. www.mywebsite/computer.html/

Is this correct?

Yes, this is working as desired. I would recommend removing the .html extension from everywhere in your install as this is not needed.

Thanks

You'd think magento would implement a solution for this?

If you change your .htaccess the canonical references still point to http://yourinstall.com/home

I know it all works fine 'cos of the 301 redirect and I'm just being picky, but wordpress sorts all this stuff just fine.

Thanks Mark - as a magento newbie, creating extensions is probably a bit beyond me just now :-)

Hi Mark,

Adding this didn't add the trailing slash to CMS pages like about-us and contact. Actually, I manually delete the trailing slash and loaded the page to see if the slash reappears, but it didn't work. Not sure how this is suppose to work, but I expected it to work like www rewrites--when I manually delete the "www." subdomain prefix, it get's added back on that page load. I have Magento in a folder, so that tricked me up, too. Anyway, here's my htaccess:

RewriteBase /newsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !=www.mysite.com
RewriteCond %{HTTPS}>s ^(on>(s)|[^>]+>s)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http%2://www.mysite.com/newsite/$1/ [R=301,L]

Note, that if you have an iPhone app or use the xmlconnect feeds, you must modify the lines to the following:
{syntaxhighlighter brush:bash;}
############################################
## add trailing slashes to url's if they don't exist

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !xmlconnect(.*)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
{/syntaxhighlighter}

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

Note: RewriteCond %{REQUEST_URI} !.html – actually ensures the rewrite will not affect the pages with a .html extension. You can change this to .php, in case you have .php extensions.

This worked for me. Hope it helps you too.

This indeed adds a trailing slash to the end, BUT it breaks the layered price navigation in my store.. Does anyone else have problems with this too?
If I want to filter by price the url parameter is '?price=2%252C10' instead of 'price=1%2C10'

I'm finally used this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.html
RewriteCond %{REQUEST_URI} !.php
RewriteCond %{QUERY_STRING} !price
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

to prevent rewrite if price param is present.

best regards,

steven

This is a great method, thank you very much for sharing. Is there a way that I can apply this on just specific pages? I currently need to 301 redirect the about us , contact us etc page to a version with ending / trailing slash. Some expert advice would be greatly appreciated.

Thank you,
Daniel