How to redirect non www to www for WordPress

how to redirect www to non www on WordPress

There are no rules about which version ( “WWW” or “non-WWW”) of your domain name you should use. It's all a matter of personal preference.

But choose you must! But, since a lot of you are just here for the code, we'll get to the point. If you're interested in learning more about how this works and why it's important, you can skip past the code for now.

Redirecting WWW to non-WWW

First, update WordPress settings
Any time your site's domain changes, make sure to update “WordPress Address” and “Site Address” under “Settings” > “General”.

Next, find and open your .htaccess file
You'll need to use some sort of file manager or FTP client to access and edit .htaccess. If you aren't comfortable doing this, contact your hosting support.

Lines to add to .htaccess
RewriteEngine on

# redirects www to non www - also redirects any other domain pointed here to yourdomain.com
RewriteCond %{HTTP_HOST} !^yourdomain\.com$
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]

Redirecting non-WWW to WWW

RewriteEngine on

# redirects non www to www - also redirects any other domain pointed here to www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

How it Works

Go to http://www.howtostartablog.org in a new window. Note that it automatically changes back to https://howtostartablog.org. This is the result of a 301 (permanent) redirect in my .htaccess file.

Why a Redirect is Important

Making sure that only one version of your domain can be used to access your pages is necessary for SEO (Search Engine Optimization). Neglecting to do this can cause duplicate content issues.

Without this redirect in place, the same page will be accessible via both www.yourdomain.com/coolpage and yourdomain.com/coolpage. These 2 urls might look pretty much the same, but to a computer (search engine crawler), they are two completely separate addresses.

This problem, better know as duplicate content, makes it harder for search engines to crawl and index a website. And creating barriers between search engines and your content breaks my golden rule of SEO. “Only provide your best content to search engines, and make it as easy as possible for them to access it.”

Thanks for reading HowToStartABlog.org, and free to reach out if you have any questions.

Share This: