Post by MAD King » Sun Aug 06, 2017 4:11 am

Hello,

I am not able to find the error. Maybe I am looking at it for too long and don't see the way.
I have SSL on my website, but it is not redirecting https://xxx.com to https://www.xxx.com
Instead, it gives me a https://www.www.xxx.com
I am using this code which I thought would work.

Code: Select all

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] 
Anyone who can help?

Thanks

New member

Posts

Joined
Wed Nov 19, 2014 10:55 pm

Post by artcore » Sun Aug 06, 2017 4:19 am

viewtopic.php?f=190&t=182662#p669374

You also need to redirect www along with https and the order of rewrite rules matters too. Hopefully above post helps you along

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by MAD King » Sun Aug 06, 2017 4:46 am

Thank you, but with your provided code it stays the same: https://xxx.com
It does not do anything.

New member

Posts

Joined
Wed Nov 19, 2014 10:55 pm

Post by MrPhil » Sun Aug 06, 2017 5:33 am

Are you looking for all http: to become https:, AND any domain xxx.com to become www.xxx.com? Then the following should work:

Code: Select all

RewriteEngine On
RewriteCond  %{HTTPS}  !on  [OR]
RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteRule  ^(.*)$   https://www.xxx.com/$1   [R=301,L]
If you have rewriting in front of this, it might affect it. In that case, you'll need to show all your rewriting. I also prefer to hard code the domain name in the RewriteRule, rather than using %{HTTP_HOST}, as that term could be xxx.com or www.xxx.com (an original http://www.xxx.com would be redirected to https://www.www.xxx.com). There's also the possibility that (on some servers), %{HTTPS} returns 0 or 1 instead of off or on. You may have to do some experimenting. And of course, you're on a server that processes .htaccess, and your host has URL rewriting enabled.

User avatar
Active Member

Posts

Joined
Wed May 10, 2017 11:52 pm

Post by MAD King » Sun Aug 06, 2017 5:50 am

HTTP gets already automatically redirected to HTTPS. Not per htaccess, but from the hoster.
I only need to redirect https://xxx.com to https://www.xxx.com

New member

Posts

Joined
Wed Nov 19, 2014 10:55 pm

Post by MrPhil » Sun Aug 06, 2017 9:52 am

So, %{HTTPS} is guaranteed to always be on? I've never seen a host do that, but I suppose it's possible. You would not need the first RewriteCond. Even if I type in http://xxx.com, you will see it as https://xxx.com (and it gets a 301 back to my browser)? Normally a host does not do this, and it's up to you in .htaccess to fix it when people typed in (or used an old bookmark or search engine index) with http:. And don't forget to update your OC configuration files so that https: is used in every link.

Anyway, if you are getting https://xxx.com changed to https://www.www.xxx.com, it sounds like your RewriteCond is always triggering, even if on the second pass of the .htaccess file (the [L] flag says to leave, and start processing .htaccess all over again). At this point, %{HTTP_HOST} should be www.xxx.com, so the RewriteCond should not trigger. By the way, all you need is !^www\., and not the (.*)$ part. Then, in the RewriteRule, I strongly suggest not using %{HTTP_HOST} (which should be www.xxx.com the second time around), but hard coding just www.xxx.com.

Code: Select all

RewriteCond %{HTTPS} on                <<< if it reaches here with http:, it will not be changed to https://www.xxx.com!
                                       <<< that's why I used !on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.xxx.com/$1 [R=301,L] 

User avatar
Active Member

Posts

Joined
Wed May 10, 2017 11:52 pm

Post by MAD King » Sun Aug 06, 2017 11:06 pm

I use Siteground and the SSL is always on.

Here is the htaccess code which is in my file, but I think this comes standard with OC

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

Code: Select all

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

New member

Posts

Joined
Wed Nov 19, 2014 10:55 pm

Post by MrPhil » Mon Aug 07, 2017 8:30 am

OK, so if (due to your host's settings), it's always https when it gets to you, you can omit the %{HTTPS} check. All you want to add to the standard .htaccess is to change xxx.com to www.xxx.com? In .htaccess, after RewriteEngine On, just

Code: Select all

# force www. form of domain name
RewriteCond  %{HTTP_HOST}  !^www\.   [NC]
RewriteRule  ^(.*)$  https://www.xxx.com/$1  [R=301,L]
That ought to do it. If it doesn't, you need to work with your host tech support to see what's going on.

Of course, you need to have your store's links already in https form and www.xxx.com form, and need to test what happens when someone just types in http instead of www.

User avatar
Active Member

Posts

Joined
Wed May 10, 2017 11:52 pm

Post by MAD King » Mon Aug 07, 2017 10:47 pm

Awesome. Works perfect. Thank you so much for helping.
There should be kind of kudos to give.

New member

Posts

Joined
Wed Nov 19, 2014 10:55 pm
Who is online

Users browsing this forum: No registered users and 153 guests