Apache two redirect conditions in one redirect rule

example.com , now what I need is request comes to example.com and www.example.com redirect to https://www.example.com,

This is my apache redirect code

    RewriteEngine On
   RewriteCond %{HTTP_HOST} ^example.com$  [NC]
    RewriteRule ^(.*)$ https://www.example.com$1 [R=301,L]

If I add RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

browser says too many redirects or redirect loop, how to solve this, I have sub domain configuration also so the condition not to affect subdomain redirects...


You don't need mod_rewrite for this

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    Redirect "/" "https://www.example.com/"
</VirtualHost >

But if you really want to use mod_rewrite

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://www.example.com/%{REQUEST_URI} [R=301,L]
</VirtualHost >