How to Redirect WWW to non WWW WordPress [htaccess snippet]


How to Redirect WWW to non WWW WordPress [htaccess snippet]

Below are two code recipes for your htaccess file to redirect different versions of your domain. Once you have things added you can use this redirect testing tool to verify all the versions of your domain are redirecting properly.

If you don’t want the WWW

Redirect everything to naked domain https://yourdomain.com

# BEGIN Redirects
RewriteEngine On
# 301 redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# 301 redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Redirects

 

If you want the WWW

Redirect everything to https://www.yourdomain.com

# BEGIN Redirects
RewriteEngine On
# 301 redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 301 redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Redirects

 



Written by: Jake Lett
Jake Lett is a B2B marketing consultant with over 15 years of experience in the digital marketing industry. He specializes in SEO, HubSpot, and PPC campaign management. Jake has a proven track record of helping businesses increase their online visibility and drive more traffic, leads and sales. He is a Certified Google Ads Specialist and a Certified HubSpot Developer.

Topic:

Related posts

Tags: ,

Want to Get Email Updates of New Articles?

Join My Email Newsletter