A Beginner’s Guide to Redirect non-www URLs to www or www URLs to non-www Using .htaccess

By | October 23, 2019

redirect non-www URLs to www

 

OpenĀ  or create a .htaccess file in your site root path

As our blog site. the root path is blog, which means this folder stores all files of blog site.

Redirect non-www URLs to www URLs

You can add code below to your .htaccess file and save.

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

Here you should replace www.yoursite.com to your domain.

Redirect www URLs to non-www URLs

You can add code below to your .htaccess file to redirect.

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

Save this file, then you can open your site to test. You will find .htaccess file works.

Leave a Reply