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

Better Stack Team
Updated on October 7, 2024

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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github