How to Get Apache on MAMP Pro to only server HTTPS

computerlarry
computerlarry used Ask the Experts™
on
I would like my Apache server to automatically connect all requests for a specific website using https:

I use MAMP Pro 5, running on Mac OS X 10.11.6  I have set up multiple websites, and for one specific domain I have successfully installed a SSL certificate that I purchased from namecheap.com
I can connect to the website securely if I type in the complete address, with https://

If I connect to the website without typing in https://  MAMP PRO's Apache server only serves up the pages without the secure connection

How can I have MAMP/Apache only connect visitors securely?
I've tried Allow http connections, but that doesn't redirect connections from http to https

MAMP Pro SSL settings for domain
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dr. KlahnPrincipal Software Engineer

Commented:
We see this question every couple of weeks.  What you want to do requires mod_rewrite, in a config file or in a .htaccess file.  Here are links to the most recent responses:

https://www.experts-exchange.com/questions/29122361/forcing-site-to-https-and-working-both-for-www-or-non-www.html
(see lenamtl solution)

https://www.experts-exchange.com/questions/29002206/Apache-Rewrite-HTTP-To-HTTPS.html
(see DrDamnit solution)

Author

Commented:
Both of the links refer to what I'll call stand-alone Apache installs.  MAMP appears to relocate the files I need to change.  I can't find them to look at the Virtual Hosts.
Fractional CTO
Distinguished Expert 2018
Commented:
You do this in your Apache config file, rather than the MAMP dashboard.

Something like this template I use for sites. You'll have to modify this slightly.

<VirtualHost *:80>
   ServerName  www.WEBSITE
   ServerAdmin support@WEBSITE
   RewriteEngine on
   RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
   RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [NC,L,R=302]
   Include logging.conf
</VirtualHost>

<VirtualHost *:80>
   ServerName  WEBSITE
   ServerAdmin support@WEBSITE
   RewriteEngine on
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=302]
   Include logging.conf
</VirtualHost>

<IfModule mod_ssl.c>

   <VirtualHost *:443>

      ServerName  www.WEBSITE
      ServerAdmin support@WEBSITE

      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
      RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [L,R=302]

      Include logging.conf

      SSLEngine on
      SSLUseStapling on

      SSLCertificateFile    /etc/letsencrypt/live/WEBSITE/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/WEBSITE/privkey.pem

      # Enable HTTP Strict Transport Security with a 2 year duration
      Header always set Strict-Transport-Security "max-age=63072000; preload"

   </VirtualHost>

   <VirtualHost *:443>

      ServerName  WEBSITE
      ServerAdmin support@WEBSITE

      DocumentRoot /sites/PROJECT/WEBSITE/TYPE

      <Directory /sites/PROJECT/WEBSITE/TYPE>
          Options +Indexes +FollowSymLinks
          AllowOverride All 
          Require all granted
      </Directory>

      Include logging.conf

      SSLEngine on
      SSLUseStapling on

      SSLCertificateFile    /etc/letsencrypt/live/WEBSITE/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/WEBSITE/privkey.pem

      # Enable HTTP Strict Transport Security with a 2 year duration
      Header always set Strict-Transport-Security "max-age=63072000; preload"

   </VirtualHost>

</IfModule>

Open in new window


Start with 302s rather than 301s, to avoid browser caching till after your entire setup is correct.

Also comment out HSTS, till entire setup is correct.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Is this the MAMP from  OS X Server or from mamp.info?

The apache from OS X Server is located in the standard place /etc/apache2/ as is typical of many Linux distros.

Author

Commented:
It is the MAMP from mamp.info

I looked at the file in apache2  and it hadn't been modified.
The MAMP documentation shows that templates reside in your ~/Library/Application Support/appsolute/MAMP PRO/templates
https://documentation.mamp.info/en/MAMP-PRO-Mac/Menu/File/#edit_templates

and the http.conf file is in /Library/Application Support/appsolute/MAMP PRO/conf/

Author

Commented:
I made the changes below, and this simple Redirect is taking care of the redirect at the moment.
My job was to get a simple site to be redirected to https.  I will deal with the 301 issues should they be reported to me.

I contacted MAMP support, and they told me to implement this code in the httpd.conf

<VirtualHost *:80>
    ServerName www.example.com
    Redirect / https://www.example.com/
</VirtualHost>

--------------------------------------------------------------

I added:

<VirtualHost *:80>
    ServerName example.com
    Redirect / https://www.example.com/
</VirtualHost>

When I had set up the domain in MAMP, the virtual host in the  https-ssl.conf    had already been taken care of, so I didn't have to do anything to that template.

Author

Commented:
I appreciate the advanced looks ahead, and I will refer to those solutions should the need arise.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial