On VPS, it seems I can use <VirtualHost>
to redirect any subdomain to main domain. On shared host, since I cannot edit the apache configuration file, how can I do?
Currently, I create a CNAME record for *.mydmain.com to redirect to mydomain.com. But When I visit nonexist.mydomain.com, it lands on nonexist.mydomain.com/cgi-sys/defaultwebpage.cgi.
You normally need to do this with the control panel level. For example Cpanel, DirectAdmin, etc.
Refers to the user manual for them and/or contact your Shared Hosting Provider. As it's a function concern, it's industry standard to include this as part of the contract for the service.