To redirect www to non-www using Nginx on Ubuntu 14.04, you can modify the server block configuration. Here’s a step-by-step guide:
-
Open Nginx Configuration File
sudo nano /etc/nginx/sites-available/default -
Modify Server Block Configuration
Find the server block that handles the domain. Within that block, add or modify the configuration to perform the redirection.
For example:
server { listen 80; server_name www.yourdomain.com; return 301 $scheme://yourdomain.com$request_uri; } server { listen 80; server_name yourdomain.com; # Other configuration directives for your domain # ... } -
Save and Close the File
Save the changes and close the editor.
-
Test Nginx Configuration
Before applying changes, ensure that your Nginx configuration is error-free:
sudo nginx -t -
Restart Nginx
If the test is successful, restart Nginx to apply the changes:
sudo service nginx restart -
Clear Browser Cache and Test
Clear your browser cache or use a different browser to test the redirection by visiting
www.yourdomain.com. It should automatically redirect toyourdomain.com.
Please replace yourdomain.com with your actual domain name in the configurations provided above.
This setup will redirect any requests coming to www.yourdomain.com to yourdomain.com. Adjust configurations according to your specific setup if you're using HTTPS or have additional server blocks or configurations for your domain