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
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
I found this
https://www.digitalocean.com/community/ ... n-on-nginx
And everestmx replied this on August 9, 2014:
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
in the .htaccess file in the /var/www/html folder, I got this after typing this command # curl -I http://www.hanzicode.comRewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
So it works in stripping off www in URL, but not in redirecting to httpsHTTP/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/
I found this
https://www.digitalocean.com/community/ ... n-on-nginx
And everestmx replied this on August 9, 2014:
This seems to be the global solution for all sites, but which file I should put this code in?# 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;
}