Page 1 of 1

How to do SSL "http to https" and "www to non-www" redirect?

Posted: Tue Jan 17, 2017 6:07 am
by lemonadv
Hi, I just install SSL certificate to my OpenCart installation.

I want to redirect

http://www.domain.com to https://domain.com
Header check: HTTP/1.1 301 Moved Permanently

http://domain.com to https://domain.com
Header check: HTTP/1.1 301 Moved Permanently

https://www.domain.com to https://domain.com
Header check: HTTP/1.1 301 Moved Permanently

Finally:
https://domain.com
to have header check: HTTP/1.1 200 OK

I made it with this code:

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

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]


I'm not sure if this is the right way?
Is there any more simple or right way to do that kind of redirect?

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Tue Jan 17, 2017 5:29 pm
by thekrotek
More simple way? What can be more simple, than adding a few lines to a single file? :-)

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Tue Jan 17, 2017 7:02 pm
by artcore
You can do it in 3 lines. www to non-www and http to https:

Code: Select all

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://yourdomain.com%{REQUEST_URI} [R=301,L,NE]

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Tue Jan 17, 2017 9:56 pm
by lemonadv
Thank you, artcore!
It works perfect!

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Tue Jan 17, 2017 10:33 pm
by artcore
You're welcome,
Cheers

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Sun Jan 29, 2017 4:38 am
by ozipekadem
artcore wrote:You can do it in 3 lines. www to non-www and http to https:

Code: Select all

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://yourdomain.com%{REQUEST_URI} [R=301,L,NE]

thank you thats works for me but this time i give error undefined in admin panel and changing any order status. how can i fix this?

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Sun Jan 29, 2017 4:44 am
by uksitebuilder
Make sure you have changed ALL URLs in your config.php and admin/config.php to be your SSL URL

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Fri Feb 03, 2017 12:13 am
by ozipekadem
not working :/
uksitebuilder wrote:Make sure you have changed ALL URLs in your config.php and
admin/config.php to be your SSL URL
config.php:

Code: Select all

<?php
// HTTP
define('HTTP_SERVER', 'http://kasecim34.com/');

// HTTPS
define('HTTPS_SERVER', 'https://kasecim34.com/');

admin/config.php:

Code: Select all

<?php
// HTTP
define('HTTP_SERVER', 'http://kasecim34.com/admin/');
define('HTTP_CATALOG', 'http://kasecim34.com/');

// HTTPS
define('HTTPS_SERVER', 'https://kasecim34.com/admin/');
define('HTTPS_CATALOG', 'https://kasecim34.com/');
davidweb09 wrote:By Placing 301 code in your site(HTTP) htaccess file you can easily redirect your site(HTTPS) to a new address...

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://domainname.com%{REQUEST_URI} [R=301,L,NE]
thicode already placed.

Re: How to do SSL "http to https" and "www to non-www" redirect?

Posted: Sat Jul 07, 2018 1:37 am
by itsmarttricks
.htaccess redirect www to non-www with SSL/HTTPS
I've got several domains operating under a single .htaccess file, each secured via SSL. I need to force https on every domain while also ensuring www's redirect to non-www's. Here's what I'm using which doesn't work:

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST}
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]

Example: https://www.itsmarttricks.com/

should redirect to... https://itsmarttricks.com ::)

Re: How to do SSL "http to https" and "www to non-www" redirect?

Posted: Sat Jul 07, 2018 11:17 pm
by thekrotek
I'd really recommend using Google for this question. There're multiple definitions for .htaccess out there, some of the may work, some not. Stack Overflow has tons of solutions.

Re: How to do SSL "http to https" and "www to non-www" redirect?

Posted: Sun Jul 08, 2018 1:48 am
by MrPhil
itsmarttricks wrote:
Sat Jul 07, 2018 1:37 am
.htaccess redirect www to non-www with SSL/HTTPS
I've got several domains operating under a single .htaccess file, each secured via SSL. I need to force https on every domain while also ensuring www's redirect to non-www's. Here's what I'm using which doesn't work:

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST}
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]

Example: https://www.itsmarttricks.com/

should redirect to... https://itsmarttricks.com ::)
Why would you expect the string "<HTTP_HOST>" to ever match "www.<HTTP_HOST>"? It never will. %{HTTP_HOST} is whatever the visitor typed in for the domain name: domain.com or www.domain.com. Try this for each domain:

Code: Select all

RewriteCond  %{HTTP_HOST}  ^domain1\.com
RewriteCond  %{HTTPS}  !on  [OR]
RewriteCond  %{HTTP_HOST}  ^www\.domain1\.com   
RewriteRule  ^(.*)$   https://domain1.com/$1  [R=301,L]
I think that should work on most servers. Note that you need to understand what format each domain comes in as... if maindomain.com/add-on.name/, you'll need to do something different.

Re: How to do SSL "http to https" and "www to non-www" redirect?

Posted: Sun Jul 08, 2018 1:57 am
by thekrotek
MrPhil wrote:
Sun Jul 08, 2018 1:48 am
I think that should work on most servers. Note that you need to understand what format each domain comes in as... if maindomain.com/add-on.name/, you'll need to do something different.
You don't actually need to define a domain in .htaccess. For me this definition works:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
See? No domain!

Re: How to do SSL "http to https" and "www to non-www" redirect?

Posted: Sun Jul 08, 2018 9:49 pm
by MrPhil
That may work for setups with only one domain in action, but my concern is add-on domains and subdomains, especially if they are presented in the form "protocol://maindomain/subdirectory/". You don't want to do a 301 redirect on an add-on or sub-domain and have the customer see the subdirectory form! Just be careful to test any form of the rewrite code that it doesn't expose the underlying structure. If it is now showing the subdirectory form, you'll need to add a redirect to the add-on or sub-domain form (still in one redirect so you aren't penalized), which probably means one section per add-on or sub-domain (looking at the %{REQUEST_URI} to see the subdirectory).

Re: How to do SSL "http to https" and "www to non-www" redir

Posted: Mon Jul 16, 2018 8:08 pm
by neosphere
artcore wrote:
Tue Jan 17, 2017 10:33 pm
You're welcome,
Cheers
Great work. This helped me too....!!!!