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.
Leave a Reply