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!

Browse topics

One more rabbit hole?

masthead-post2-18

March 14, 2026

Before You Add AI to WordPress, Build a Vendor-Exit Plan

As WordPress moves toward shared AI connectors and provider packages, site owners need one practical habit: choose tools you can leave without wrecking content or workflows.

Read article ↗

masthead-post2-8

February 26, 2026

7 non-dev habits that protect your WordPress site from XSS

XSS keeps showing up year after year. Here are seven practical, non-technical habits that shrink your WordPress risk without turning you into a security engineer.

Read article ↗