Apache
If your VPS is running the Apache web server, follow these steps to redirect non-www URLs to www:
Connect to Your VPS: Ensure you have root access with sudo privileges.
Enable mod_rewrite
: Run the following command:
sudo a2enmod rewrite
Restart Apache:
sudo systemctl restart apache2
Enable .htaccess
:
Create .htaccess
File:
- Add the following lines:
![](https://www.community.vpssell.com/assets/files/2024-07-31/1722399300-129489-5.png)
Restart Apache Again:
sudo systemctl restart apache2
To create the .htaccess file, use this command:
sudo vi /var/www/html/.htaccess
- Then add the following lines to the file:
![](https://www.community.vpssell.com/assets/files/2024-07-31/1722399654-601023-8.png)
Replace yourdomain.com with your actual domain name and save the file
Nginx
If your VPS is running the Nginx web server, follow these steps to redirect non-www URLs to www:
Log in to Your VPS via SSH.
Navigate to Nginx Directory:
View Directory Content:
sudo ls -la
Next enter your NGINX password:
Edit nginx.conf
File:
sudo nano nginx.conf
Add the following lines, replacing yourdomainname.com
with your actual domain name:
![](https://www.community.vpssell.com/assets/files/2024-07-31/1722400130-588451-6.png)
Restart Nginx:
sudo systemctl restart nginx
Additional Nginx Redirects
In addition to redirecting non-www to www URLs, you can configure other redirects in Nginx, such as HTTP to HTTPS and other page redirects.