In this post I am going to explain to you how we can redirect a non-www website to www website or www to non-www on Apache server in simple steps.

Step 1. Before we start to hope you already enable mod_rewrite. If not then run below command

Step 2. Now open your site configuration file and update code inside the directory tag.

Step 3. Now go to your project directory and create a .htaccess file in it.

Redirect non-www website to www in Apache server

To do redirection copy paste below code in .htaccess file

Redirect www website to non-www in Apache server

That’s it hope this code helps you.