Page 1 of 2
non-www to www redirect
Posted: Tue Jul 12, 2011 7:32 pm
by Tromas
I discovered that if customers go to example.com instead of
www.example.com, the add to cart button don't work (1.5.05 version) on the front page.
Does the following code look ok? I added to two last lines.
I' just not sure if it's ok to use two RewriteRules.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Re: non-www to www redirect
Posted: Tue Jul 12, 2011 9:24 pm
by MattW
This is how I have mine:
Code: Select all
RewriteCond %{HTTP_HOST} ^z22se.co.uk
RewriteRule (.*) http://www.z22se.co.uk/$1 [R=301,L]
Re: non-www to www redirect
Posted: Tue Jul 12, 2011 11:41 pm
by webpie it.
I have been old before that this could be down to a jquery thing where it is only set to
www.example.com with the prefix.
I advice you do what MattW has done as this fixed my issue with jquery and missing WWW. prefix
Re: non-www to www redirect
Posted: Sat Oct 08, 2011 10:00 pm
by Tromas
I have tried MattW's method, but then I couldn't log into my admin panel.
I get the log in screen, but nothing happens when I try to log in.
Anything I need to change in config.php in my admin map?
Edit: solved
just had to add some www. in the config.php (admin map).
Re: non-www to www redirect
Posted: Sat Oct 08, 2011 11:52 pm
by uksitebuilder
Here is the code I use - Hope it helps
Code: Select all
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Re: non-www to www redirect
Posted: Sat Nov 26, 2011 2:10 am
by hoo91
My OpenCart is installed in a '/shop' directory and I'm having a problem using this code below:
RewriteBase /shop
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Problems I'm seeing:
1.) When I type my domain without WWW, it doesn't redirect to my domain with WWW.
2.) When I type my domain without WWW, but also include the '/shop' directory to access the OCart, it redirects back to
http://www.mydomain.com without the '/shop' directory.
Thanks for the help.
*******EDITED*********
I just figured out solution for #2 above:
When I change
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] to
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/shop$1 [R=301,L] (adding the /shop) it correctly redirects when typing the /shop directory.
Still can't figure out how to solve #1 though. Help on that appreciated.
Re: non-www to www redirect
Posted: Sat Nov 26, 2011 2:36 am
by uksitebuilder
That's because RewriteBase should have a trailing slash
i.e.
RewriteBase /shop/
not
RewriteBase /shop
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 11:56 am
by hoo91
Thanks for your reply. I changed /shop to /shop in my htaccess file and that didn't solve my problem. The redirect works perfectly when i access my site with the /shop directory or any directories within the /shop directory. But if I go to my domain without the /shop directory and without typing www, it does not redirect correctly. Any other suggestions?
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 4:08 pm
by uksitebuilder
Here is the Rewrite section that I would use
RewriteEngine On
RewriteBase /shop/
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 7:15 pm
by emilygraham
Hi Simon,
I have tried using the code you printed above to redirect from non-www to www, whilst still retaining SEO friendly URLS, but it is not working as expected and not transferring from non-www to www. Is there anything else that needs configuring? My config files say
http://domainname.co.uk... as opposed to
http://www.domainname.co.uk, but changing this resulted in a 'page not found error' Any ideas?
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 7:25 pm
by uksitebuilder
Hi,
Well your config file should read as follows:
the trailing slash is important
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 7:27 pm
by emilygraham
Hi Simon,
My config files currently read using the format:
define('HTTP_SERVER', '
http://domain.co.uk/');
If i change that to:
define('HTTP_SERVER', '
http://www.domain.co.uk/');
i get a 'page not found error' when viewing the site
does the prefix www. need to be set anywhere else?
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 7:30 pm
by uksitebuilder
you missed the /shop/ off the end
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 7:35 pm
by emilygraham
even if my website is at the root level (i.e not under the folder 'shop')?
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 8:00 pm
by emilygraham
Its actually not a 'page not found error' but a 'oops! This link appears to be broken' error. Typing the URL
http://domain.co.uk/designers works fine, but the URL
http://www.domain.co.uk/designers gives the 'Oops! This link appears to be broken' error - makes me think that somewhere i am specifying a non-www URL but no idea where???
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 8:36 pm
by emilygraham
Hi Simon,
Made a bit more progress now - appologise, i was missing a line in the .htaccess file. All redirects are working fine now and the SEO friendly URLs. Using the code you pasted though (copied my version below), i am not able to login to administration though which is obviously causing me some problems !?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Many thanks for your help
Emily
Re: non-www to www redirect
Posted: Mon Nov 28, 2011 8:55 pm
by emilygraham
Would you believe me if i now said it was all working correctly !?! Able to login no problems, maybe something was cached....thanks for your help
Solution to admin access problems with redirect 301
Posted: Tue Jan 17, 2012 6:30 pm
by albsim81
When you install opencart you have to choose between www or non-www url.
Probably you chose non-www so now you must choose the same in .htaccess
Here the solution
http://forum.opencart.com/viewtopic.php?f=20&t=29515
Re: non-www to www redirect
Posted: Sat Jul 06, 2013 9:45 am
by newtress
While this seems to work well to help the code redirect, how do you avoid getting subdomains to redirect as with the rest of the site. For example if you have the subdomain 'test' at
http://test.domain.com, how do you avoid it from becoming
http://www.test.domain.com, which for obvious reasons will not work.
Re: non-www to www redirect
Posted: Sat Jul 06, 2013 9:55 am
by newtress
Nevermind, I just figured this out. It seems all you have to do is create a condition that places the sub-domain in a no case rule to stop it from being part of the decision, as below, based on your subdomain of course.
RewriteCond %{HTTP_HOST} !^subdomain\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Ya, glad we figured that out. Now you can see our redirects at
http://newtress.com. It should go to
http://www.newtress.com.