.htaccess 301 redirect Homepage to subdomain

I would like to redirect my home page to a subdomain in .htaccess. Ive tried several different ways I found online. None have worked.

https://example.com/index.com to https://sub.example.com/index.com

The code below is how you would do it!

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ “https://sub.example.com/” [R=301,L]