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 towww.example.com
and issues a 301 (permanent) redirect toexample.com
. - The second
server
block 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
server
block listens for requests toexample.com
and issues a 301 redirect towww.example.com
. - The second
server
block 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.com
toexample.com
. - The second block redirects
example.com
towww.example.com
. - The third block serves content and ensures that the
www
andnon-www
domains 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
www
tonon-www
: Use a server block withserver_name www.example.com
to redirect requests toexample.com
. - Redirect
non-www
towww
: Use a server block withserver_name example.com
to 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.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github