Add to cart not working on opencart homepage

Posted by Flo on 08 May 2012

A very common problem that many users encounter is the Add to Cart sometimes not working on their site homepage.

The problem is related to the url of the store. A website can be accessed trough adress like: example.com as well as www.example.com

When you first install opencart, the base of the website is going to be written in config.php, but it can only be one: either example.com or www.example.com (depends on the adress you accessed the installation in the first place). So if you install your store by going to example.com, then the base of your website in config.php will be example.com. So here is the origin of the problem, because if you then access your store via www.example.com , the add to cart wont work on the homepage.

To fix this problem, you can make a redirect in htaccess from www.example.com to example.com or the other way around (depends on which case applies to you).

First, to make a redirect from non www to  www adress (ex: from example.com to www.example.com), edit the .htaccess from your opencart root and find the line:

 

RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

 

and below it add the code:

 

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

 

To make a redirect from www to non www adress (ex: from www.example.com to example.com), edit the same file mentioned above, and below the same line mentioned, add the code:

 

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

 

In both cases, replace example.com with the adress of your store.