apache 2.4 – Redirecting all versions of URL to main site with 301 status code


I want to permanently redirect all requests starting with the following to https://www.example.com using 301 code.

http://example.com

http://www.example.com

https://example.com

How should I edit my apache config files? Or do it using .htaccess file?

I currently have this in my apache config for http site:

<VirtualHost 5.x.x.x:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>

and this in my https config:

<VirtualHost 5.x.x.x:443>
    ServerName example.com
    Redirect permanent / https://www.example.com/

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/example.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>

<VirtualHost 5.x.x.x:443>
    ServerName www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /home/example/public_html/public
    UseCanonicalName Off

    ...
    FULL CONFIG
    ...
</VirtualHost>

But a Seo test website (woorank) says the URLs resolve to different addresses (4th redirection is different and redirects to uppercase resource name?):

URL Resolve
Warning, no 301 redirects are in place to redirect traffic to your preferred example. Pages that load successfully both with and without www. are treated as duplicate content!
Not all versions of your page point to the same URL.

http://example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7 
to  
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

http://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7 
to  
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

https://example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7
to
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

https://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7
to
https://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7

Thank you