avatar Deluxe Blog Tips About Projects

.htaccess redirect www to non-www

I usually use non-www URLs for simplicity and having shorter URLs. So, I often redirect all requests to www to their non-www versions. This snippet redirects all requests to www.example.com to example.com. It's generic, works with all domains and doesn't require you to write the domain name.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1$1 [R=301,L]

It should be in your .htaccess file in your web root folder, or in your virtual host configuration file. Note that it works only with Apache.

If you're not familiar with editing .htaccess file, and you're using WordPress, then use the Slim SEO plugin. It has a redirection module which supports redirecting www to non-www and vice-versa.

Redirection settings in Slim SEO

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments