fix the opencart "add to cart button" notworking bug:
http://shopencart.com/add-to-cart-not-w ... ome?page=2
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
example.com as well as
http://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
http://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
http://www.example.com , the add to cart wont work on the homepage.
To fix this problem, you can make a redirect in htaccess from
http://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
http://www.example.com), edit the .htaccess from your opencart root and find the line:
Code: Select all
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
and then add this:
Code: Select all
# FORCE WWW IN URL
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# END FORCE WWW IN URL
if your cart is in a subfolder like example.com/cart/ do this:
Code: Select all
# FORCE WWW IN URL
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/cart/$1 [R=301,L]
# END FORCE WWW IN URL
to remove the www from the url
Code: Select all
# FORCE nonWWW IN URL
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]