Forcing www or non-www in WordPress with an htaccess

August 24, 2017

Just for personal future reference, but feel free to use if it helps you too!

Using the .htaccess file, you can force www or non-www URLs using the following snippets that should appear ABOVE any other code in your WordPress .htaccess.

Forcing www

Open up the .htaccess file in the root directory of your WordPress install and add the following to the top of the file. (Make sure you replace example.com with your domain.)

RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Forcing non-www

Like the other snippet, open up the .htaccess and paste this snippet above everything else. (Make sure you replace example.com with your domain.)

RewriteEngine On
# Redirect www urls to non-www
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Wanna chat about this article or any others? Feel free to DM me or mention me on Twitter @marcusdburnette to start a conversation!

Leave a Reply

Your email address will not be published. Required fields are marked *