0

I have virtual host on Apache that has configurations similar to the following:

<VirtualHost *:80>    
    DocumentRoot "/home/doe/www/factory/public"
    <Directory "/home/doe/www/factory/public">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    ServerName doe.example.com
    ServerAlias qa.dev
    ServerAlias doe202.ddns.net
    ErrorLog "/home/doe/logs/doe.log"
    CustomLog "/home/doe/logs/aq-access.log" common    
</VirtualHost>
<VirtualHost *:443>   
    DocumentRoot "/home/doe/www/factory/public"
    <Directory "/home/doe/www/factory/public">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    ServerName doe.example.com
    ServerAlias qa.dev
    ServerAlias doe202.dddns.net
    ErrorLog "/home/doe/logs/doe.log"
    CustomLog "/home/doe/logs/aq-access.log" common  
    SSLEngine on        
      SSLCertificateFile  /etc/letsencrypt/live/doe.example.com/fullchain.pem
      SSLCertificateKeyFile  /etc/letsencrypt/live/doe.exmple.com/privkey.pem
</VirtualHost>

What I need to achieve is to redirect only http://doe.example.com to https://doe.example.com of course with any URI attached to it.

I have tried to look for this answer of that question: http to https apache redirection, but it will redirect all hosts to HTTPS.

I also tried to change serverName directive in port 80 virtual host, but this will prevent users that use http from access at all.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195

1 Answers1

0

I have found a solution depends on the solution used to redirect www to non www with hard redirect like the following:

RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^doe\.example\.com$ [NC]
    RewriteRule ^(.*)$ https://doe.example.com/$1 [R=301,L]
# ... the rest of .htaccess

In this way, only http://doe.example.com will be redirected to https://doe.example.com while other aliases will keep its protocol as it is requested.

By the way, the website is project, and all .htaccess configurations of the project comes next to the configurations above.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195