October 13, 2019

How to redirect www to non-www with .htaccess on your site

Back to Solutions

For search engine optimization, you will need to choose only one version of your domain name to prevent duplicate content. That could be either www.example.com or the non-www version example.com

To redirect one to other, you will need to make sure Mod_Rewrite is enabled on your server. Edit your .htaccess file and add the code below. Note that in this case, the www version is redirected to non-www and you will need to replace your domain name below.

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

If you already have a .htaccess file with RewriteEngine On copy only the following code after that line.

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

Was this solution helpful?

Related Solutions

7-Day Website

Professional site, live in one week. Built to convert visitors into customers.

Your website. Live in 7 days or less. Starting at $999.

Enterprise Solutions

Complex requirements? We've built platforms handling 10M+ requests daily.

Let's architect something your competitors can't copy.

GemPixel