preloader

Redirect www to non-www and non-www to www in Apache

After configuring multiple websites on a single server, it is always difficult to point the domain name with their respective installations. Sometimes www.yourdomain.com will work and yourdomain.com will not work, similarly sometimes yourdomain.com will work but www.yourdomain.com will not work. To resolve the issue you need to add the multiple virtual hosts in your configuration file located at ‘/etc/apache2/sites-available/yourdomain.com.conf’. Below is the sample configuration file.

<VirtualHost *:80>
 ServerName www.yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>

<VirtualHost *:80>
ServerName yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>

Finally restart the apache
Sudo systemctl restart apache2

Related Post

Leave a Reply

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