Using .htaccess to Prevent www Subdomain

Looking for a better way to manage your org's technical proposals?

You can use the following rules in your .htaccess file to force users to visit your website without the www subdomain. Your users will be 301 (Permanently) redirected to the same path, but at your domain without the www.

This is a really simple thing to pull off, however I haven't seen any other websites talk about it, most people want to force users to have the www.

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

This should work with any website assuming it is hosted with Apache, and that both the normal domain and the www subdomain both resolve properly.

Thomas Hunter II Avatar

Thomas Hunter II is a Software Engineer with 18+ years building scalable, production-grade systems. Deep expertise in Node.js, observability, and developer tooling, with a strong track record of driving architectural decisions, mentoring engineers, and improving customer facing technical documentation. Author of 5 books (including O'Reilly), certified by the OpenJS Foundation, and frequent international conference speaker.