Apache
If your VPS is running Apache, you'll need to modify the .htaccess file to redirect non-www URLs to www. Here’s how to do it:
1.Connect to Your VPS: Log in as root with sudo privileges.
2.Enable mod_rewrite: Run the following command to enable the mod_rewrite module:
sudo a2enmod rewrite
3.Restart Apache: Apply the changes by restarting the Apache server:
sudo systemctl restart apache2
4.Enable .htaccess: Edit the Apache configuration file:
sudo vi /etc/apache2/sites-available/000-default.conf
5.Add the following code block before the line <Directory /var/www/html>:

6.After that, restart Apache.
7.Create or Edit .htaccess File: To create or edit the .htaccess file, use:
sudo vi /var/www/html/.htaccess
8.Add these lines to the file:

Replace yourdomain.com with your actual domain name, then save the file.
9.Restart Apache Again: Finally, restart the Apache server:
sudo systemctl restart apache2
Redirecting with NGINX
If your VPS is using NGINX, follow these steps to redirect non-www URLs to www:
1.Log in to Your VPS: Access your VPS via SSH.
2.Navigate to the NGINX Directory: Move to the NGINX directory:
cd /etc/nginx/
3.View Directory Content: Check the directory contents with:
sudo ls -la
4.Edit NGINX Configuration: Open the nginx.conf file for editing:
sudo nano nginx.conf
5.Add the following server block, replacing yourdomainname.com with your actual domain:

6.Restart NGINX: Apply the changes by restarting the NGINX server:
sudo systemctl restart nginx
In addition to redirecting non-www URLs to www, NGINX can also be used for other redirects, such as HTTP to HTTPS, and other types of page redirects.