Page 1 of 1

http (www or non-www) to https non-www on VPS Debian 9 server for all sites

Posted: Tue May 07, 2019 10:08 pm
by lotnomore
I just set up a VPS server from Hostwinds running Debian 9 and Vesta. Have spent days trying to make sure all sites with SSL certificates load this way:

https://anydomain.com

Since I have more than 50 sites on the server and trying to test on the .htaccess file in each domain's public_html folder is a pain in the neck and I also get the "too many redirects" error. I wonder if there is a better and easy way.

I have tried with this one:
https://www.digitalocean.com/community/ ... ntu-14-04/
but that is for apache server, not for the nginx server. So after implementing that by adding the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
in the .htaccess file in the /var/www/html folder, I got this after typing this command # curl -I http://www.hanzicode.com
HTTP/1.1 302 Found
Server: nginx
Date: Tue, 07 May 2019 22:03:30 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 205
Connection: keep-alive
Keep-Alive: timeout=60
Location: http://hanzicode.com/
So it works in stripping off www in URL, but not in redirecting to https

I found this
https://www.digitalocean.com/community/ ... n-on-nginx

And everestmx replied this on August 9, 2014:
# host_without_www
if ( $host ~* ^www\.(?<nowww>.*)$ ) {
rewrite .* $scheme://$nowww$request_uri permanent;
}
# host_with_www
if ( $host ~* ^(?!www\.) ) {
rewrite .* $scheme://www.$host$request_uri permanent;
}
This seems to be the global solution for all sites, but which file I should put this code in?

Re: http (www or non-www) to https non-www on VPS Debian 9 server for all sites

Posted: Tue May 07, 2019 11:06 pm
by kenneth
Try this one

Code: Select all

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Re: http (www or non-www) to https non-www on VPS Debian 9 server for all sites

Posted: Tue May 07, 2019 11:30 pm
by lotnomore
kenneth wrote:
Tue May 07, 2019 11:06 pm
Try this one

Code: Select all

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Tried that. No luck, the same response for command

Code: Select all

curl -I http://www.hanzicode.com
HTTP/1.1 302 Found
Server: nginx
Date: Tue, 07 May 2019 23:27:46 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 205
Connection: keep-alive
Keep-Alive: timeout=60
Location: http://hanzicode.com/