I am trying to write a .htaccess script that can redirect example.com to example.com/subdomain. This is my code but it's not working. Can you please suggest.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/subdomain/ [R=301,L]

Thanks in advance

Not sure you need to treat the url as a regex in your condition. This should work:

RewriteCond %{HTTP_HOST} www.example.com [NC]

Also, requests to the subdirectory itself will satisfy your condition, so you have a circular reference there. You need a new condition that causes the rule to only work on requests for resources NOT in your /subdomain/ directory.

if i'm adding www then the page is not opening