What do you mean by "redirects to 404 page"? What URL do you request? What does the address bar say when the error page is displayed?
Please use example.com in posts in this forum as it prevents the URL auto-linking function from activating. There may still be time for you to re-edit your post.
The www/non-www code is non-optimum.
The preceding condition of the second ruleset is not needed. The pattern to match is already defined in the rule.
Trailing uncaptured .* is always redundant on any unanchored pattern and can be removed.
The rule target probably needs to include the target folder name not just the index.php filename.