# Nginx No-www to Www and Www to No-www

Redirecting between `www` and `non-www` (or `no-www`) versions of your domain using Nginx involves setting up rewrite rules or redirect directives in your server configuration. You can choose to redirect `www` to `no-www` or vice versa based on your preference. Here’s how to configure each scenario:

### **1. Redirect `www` to `non-www`**

If you want all requests from `www.example.com` to redirect to `example.com`, you can use the following configuration:

### **Example Configuration**

```
server {
    listen 80;
    server_name www.example.com;

    # Redirect www to non-www
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;

    # Your usual server configuration for non-www
    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

- **Explanation**:
    - The first `server` block listens for requests to `www.example.com` and issues a 301 (permanent) redirect to `example.com`.
    - The second `server` block serves content for `example.com`.

### **2. Redirect `non-www` to `www`**

If you prefer to redirect `example.com` to `www.example.com`, use the following configuration:

### **Example Configuration**

```
server {
    listen 80;
    server_name example.com;

    # Redirect non-www to www
    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen 80;
    server_name www.example.com;

    # Your usual server configuration for www
    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

- **Explanation**:
    - The first `server` block listens for requests to `example.com` and issues a 301 redirect to `www.example.com`.
    - The second `server` block serves content for `www.example.com`.

### **3. Redirect Both `www` to `non-www` and `non-www` to `www`**

In some cases, you might want to redirect both `www` to `non-www` and `non-www` to `www` based on specific requirements or SEO considerations. Here’s how you can handle both redirects:

### **Example Configuration**

```
# Redirect www to non-www
server {
    listen 80;
    server_name www.example.com;

    return 301 $scheme://example.com$request_uri;
}

# Redirect non-www to www
server {
    listen 80;
    server_name example.com;

    return 301 $scheme://www.example.com$request_uri;
}

# Serve content for www or non-www
server {
    listen 80;
    server_name www.example.com example.com;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

- **Explanation**:
    - The first block redirects `www.example.com` to `example.com`.
    - The second block redirects `example.com` to `www.example.com`.
    - The third block serves content and ensures that the `www` and `non-www` domains are both handled correctly. The configuration here should ideally be designed based on the final redirect you wish to enforce.

### **4. HTTPS Considerations**

If you are using HTTPS, you should apply similar redirect configurations to your SSL-enabled server blocks.

### **Example HTTPS Redirect Configuration**

```
# Redirect www to non-www (for HTTPS)
server {
    listen 443 ssl;
    server_name www.example.com;

    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/key.pem;

    return 301 https://example.com$request_uri;
}

# Redirect non-www to www (for HTTPS)
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/key.pem;

    return 301 https://www.example.com$request_uri;
}

# Serve content for www or non-www (HTTPS)
server {
    listen 443 ssl;
    server_name www.example.com example.com;

    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/key.pem;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

### **Summary**

- **Redirect `www` to `non-www`**: Use a server block with `server_name www.example.com` to redirect requests to `example.com`.
- **Redirect `non-www` to `www`**: Use a server block with `server_name example.com` to redirect requests to `www.example.com`.
- **Redirect Both**: Set up two separate server blocks to handle each redirect direction and ensure proper content serving.

Ensure to test your configuration thoroughly after making changes to confirm that redirects are working as expected and that there are no infinite redirect loops.