Control Web Panel

WebPanel => Apache => Topic started by: jeffshead on August 10, 2018, 01:11:19 PM

Title: Definitive answer: HTTP to HTTPS www
Post by: jeffshead on August 10, 2018, 01:11:19 PM
I have been struggling with this for days but can't get this to work in all situations.

What is the proper way (per domian) to direct all traffic to https://www.domain.tld/:

http://domain.tld-> https://www.domain.tld/
http://www.domain.tld-> https://www.domain.tld/
https://domain.tld-> https://www.domain.tld/

I am using Apache & Varnish Cache & Nginx Reverse Proxy and AutoSSL. I have tried rewrites and redirects in .htaccess and in vhosts configs but I have been unsuccessful.

I know you can edit the vhost templates and I also see that you can have per domain, custom config files but what is the proper code and way of doing this so the configurations are not lost when CWP rebuilds the vhosts?

(https://i.imgur.com/m105sh7.gif)
Help... I'm starting to hemorrhage!
Title: Re: Definitive answer: HTTP to HTTPS www
Post by: pixelpadre on October 12, 2018, 04:55:27 PM
Today (a little late) I modified vhost template by adding this:

<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://DOMAIN_REPLACE$1 [R=301,L]
   </IfModule>

This will redirect http requests to https.  You must have https cert of course.

Also VERY IMPORTANT....you will have to delete this rewrite module every single time that you attempt to renew certs.  And then manually insert it after cert is renewed.  Easiest way to do this is to have two copies of your vhost config file.  One with the rewrite and one without the rewrite.  This will save you a ton of time if you numerous domains.  If you have only one then its just as easy to type it in each time.  ALTERNATIVELY you can comment out the rewrite with hash tags prior to cert renewal.
Title: Re: Definitive answer: HTTP to HTTPS www
Post by: jeffshead on October 12, 2018, 09:53:42 PM
Thanks for the reply. I appreciate you taking the time :-)

I think you may have overlooked that I stated I'm using the NGINX reverse-proxy. I did figure out a way to make it work though.

I edited the NGINX vhosts templates so that I now have a 301 redirect from http to https and I added an additional server instance in the NGINX ssl vhost template so I now have a domain.tld and a www.domain.tld instance. The domain.tld server has a 301 redirect to www.domain.tld.

It works great!
Title: Re: Definitive answer: HTTP to HTTPS www
Post by: pixelpadre on October 12, 2018, 11:30:13 PM
What you just said is what I posted.
Title: Re: Definitive answer: HTTP to HTTPS www
Post by: jeffshead on October 13, 2018, 10:59:44 AM
No, it's not  :) You posted an Apache redirect. I stated that I changed my NGINX config to redirect. Are you using the redirect you posted with Apache & Varnish Cache & Nginx Reverse Proxy?

I could not get any http-to-httpswww Apache redirects to work (flawlessly) behind the NGINX reverse-proxy including the one that you posted. Some would loop and some would cause server errors. I'm also behind a web server firewall appliance so maybe that has something to do with it. The NGINX redirects work flawlessly and I believe that's where the redirects should be done since incoming traffic hits NGINX before it gets to Apache.