Why does a website work with www but not without it?

www.example.com and example.com are separate DNS names. The www record may point to the server while the root domain has no A, AAAA, or provider-specific alias record. Add the root record using the destination supplied by the host and confirm that the authoritative DNS response is correct.
The hosting server must recognize both names. A domain can resolve to the correct address and still show an error or another site if the server configuration contains only www. Add the root domain as an alias or primary domain in the hosting platform, then include it in the site’s SSL certificate.
Application settings and redirects can also cause a failure. A rule may redirect the root to an invalid address, repeat the redirect, or force a hostname that the certificate does not cover. Inspect the HTTP response from both versions and test over HTTPS after clearing cached redirects in a private browser window.
Choose one version as the canonical public address and redirect the other with a permanent server-side redirect. Keep DNS and SSL working for both because visitors and search engines may request either one. Update internal links, analytics, and search settings to use the chosen version consistently.