Search code examples
apache.htaccesswebvirtualhostvirtual-hosts

htaccess https for several subdomains


I have virtual hosts

<VirtualHost 10.10.10.10:80>
     ServerAdmin [email protected]
     ServerName server.com
     ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
     DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>

<VirtualHost 10.10.10.10:443>
     ServerAdmin [email protected]
     ServerName server.com
     ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
     DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>

I want to force visitors use https for subdomain-a and subdomain-c. Visitors of subdomain-b and subdomain-d could use http and https. How to configure .htaccess?


Solution

  • Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(subdomain-a|subdomain-c)\.server\.com$ [NC]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]