How To Redirect www to Non-www with Nginx on Ubuntu 14.04

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:

  1. Open Nginx Configuration File

                    
                        sudo nano /etc/nginx/sites-available/default
                    
                

  2. 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
                            # ...
                        }                    
                    
                

  3. Save and Close the File

    Save the changes and close the editor.

  4. Test Nginx Configuration

    Before applying changes, ensure that your Nginx configuration is error-free:

                    
                        sudo nginx -t
                    
                

  5. Restart Nginx

    If the test is successful, restart Nginx to apply the changes:

                    
                        sudo service nginx restart
                    
                

  6. 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 to yourdomain.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

SSH Essentials: Working with SSH Servers, Clients, and Keys

SSH (Secure Shell) is a cryptographic network protocol that allows secure communication between two computers over an insecure network. It is commonly used for remote login and command execution but can also be used for secure file transfer and other …

read more

How To Set Up an Ubuntu Server on a DigitalOcean Droplet

Setting up an Ubuntu Server on a DigitalOcean Droplet is a common task for deploying web applications, hosting websites, running databases, and more. Here's a detailed guide to help you through the process. Setting up an Ubuntu server on a DigitalOce …

read more