Search code examples
apachesslhttpsmamp

Forward http to https on a folder in MAMP


So I have created an SSL key and a certificate for my website. I was able to forward every page of my website to HTTPS by using the following code in my httpd-vhosts.conf:

RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

That works fine for every page, but I would like to be able to forward http to https only on my Account page. I tried this code in my https-vhosts.conf:

<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
ServerName example.com
ServerAlias www.example.com/Account
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</VirtualHost>
# SSL Configuration
<VirtualHost *:443>
      DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
      ServerName example.com
      ServerAlias www.example.com/Account
      SSLEngine on
      SSLCertificateFile /Applications/MAMP/conf/apache/server.crt
      SSLCertificateKeyFile /Applications/MAMP/conf/apache/server.key

</VirtualHost>

It does not redirect to https, it just stays the standard http. My Certificate is correct so that is not the problem.

Any help is greatly appreciated.

Thanks in advance!


Solution

  • Add a second condition to your rewriting, so that it only gets applied then that condition is true. This also allows you to extract the required part of the request:

    # http Configuration
    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteCond %{REQUEST_URI} ^/Account
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        DocumentRoot "/Applications/MAMP/htdocs/Website"
        <Directory /Applications/MAMP/htdocs/Website>
            [...]
        </Directory>
    </VirtualHost>
    
    # SSL Configuration
    <VirtualHost *:443>
        ServerName example.com
        ServerAlias www.example.com
        SSLEngine on
        SSLCertificateFile /Applications/MAMP/conf/apache/server.crt
        SSLCertificateKeyFile /Applications/MAMP/conf/apache/server.key
        DocumentRoot "/Applications/MAMP/htdocs/Website"
        <Directory /Applications/MAMP/htdocs/Website>
            [...]
        </Directory>
    </VirtualHost>