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 serverblock listens for requests towww.example.comand issues a 301 (permanent) redirect toexample.com.
- The second serverblock serves content forexample.com.
 
- The first 
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 serverblock listens for requests toexample.comand issues a 301 redirect towww.example.com.
- The second serverblock serves content forwww.example.com.
 
- The first 
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.comtoexample.com.
- The second block redirects example.comtowww.example.com.
- The third block serves content and ensures that the wwwandnon-wwwdomains are both handled correctly. The configuration here should ideally be designed based on the final redirect you wish to enforce.
 
- The first block redirects 
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 wwwtonon-www: Use a server block withserver_name www.example.comto redirect requests toexample.com.
- Redirect non-wwwtowww: Use a server block withserver_name example.comto redirect requests towww.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.
