How to Redirect Non-WWW or WWW in Nginx on Linux

In today’s digital age, having a website is crucial for businesses and individuals to showcase their products and services to a wider audience. A website’s URL is the first thing a user sees, and it’s essential to have a clean and straightforward URL structure for better user experience and improved search engine optimization (SEO). Nginx is one of the most popular web servers used by many websites, and it provides an easy way to redirect non-WWW and WWW URLs.

In this article, we will be discussing how to redirect non-WWW and WWW URLs with Nginx. We will be covering everything from the basics of URL redirects to advanced techniques, including how to redirect URLs using Nginx server blocks, how to redirect URLs using Nginx redirect directives, and how to redirect URLs using Nginx rewrite rules.

Understanding URL Redirects and Why They Are Important

URL redirects are essential in creating a seamless user experience and improving SEO. Redirects help to ensure that users reach the correct page on a website, even if they type in the wrong URL or if the URL structure has changed. There are several types of URL redirects, including:

  • 301 Redirects: A permanent redirect that tells search engines that the URL has permanently moved to a new location.
  • 302 Redirects: A temporary redirect that tells search engines that the URL has temporarily moved to a new location.
  • 303 Redirects: A redirect used for form submissions, indicating that the form data should not be re-submitted when refreshing the page.

It’s important to use the correct type of redirect for the right situation. For example, if you’re redirecting a URL because the page has been permanently moved, you should use a 301 redirect.

How to Redirect Non-WWW & WWW URLs with Nginx

Redirecting URLs Using Nginx Server Blocks

Nginx server blocks allow you to define different server configurations for different domains or subdomains. To redirect non-WWW and WWW URLs using Nginx server blocks, you need to create two server blocks, one for the non-WWW URL and one for the WWW URL.

Here’s an example of how to redirect non-WWW URLs to WWW URLs using Nginx server blocks:

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen 80;
    server_name www.example.com;
    # Add your website content here
}

In this example, the first server block listens on port 80 and listens for requests to the non-WWW URL, example.com. The return directive is used to redirect the URL to the WWW URL, www.example.com. The $scheme variable is used to specify the protocol (HTTP or HTTPS), and the $request_uri variable is used to preserve the original URL path and query string.

The second server block listens on port 80 and listens for requests to the WWW URL, www.example.com. This server block is used to serve the actual content of your website for the WWW URL.

Here’s an example of how to redirect WWW URLs to non-WWW URLs using Nginx server blocks:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;
    # Add your website content here
}

In this example, the first server block listens on port 80 and listens for requests to the WWW URL, www.example.com. The return directive is used to redirect the URL to the non-WWW URL, example.com. The $scheme variable is used to specify the protocol (HTTP or HTTPS), and the $request_uri variable is used to preserve the original URL path and query string.

The second server block listens on port 80 and listens for requests to the non-WWW URL, example.com. This server block is used to serve the actual content of your website for the non-WWW URL.

Redirecting URLs Using Nginx Redirect Directives

Another way to redirect non-WWW and WWW URLs with Nginx is by using redirect directives. Nginx provides several redirect directives, including the return, rewrite, and permanent.

Here’s an example of how to redirect non-WWW URLs to WWW URLs using Nginx redirect directives:

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

In this example, the return directive is used to redirect the non-WWW URL, example.com, to the WWW URL, www.example.com. The $scheme variable is used to specify the protocol (HTTP or HTTPS), and the $request_uri variable is used to preserve the original URL path and query string.

Here’s an example of how to redirect WWW URLs to non-WWW URLs using Nginx redirect directives:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

In this example, the return directive is used to redirect the WWW URL, www.example.com, to the non-WWW URL, example.com. The return directive is used to send an HTTP response to the client with a status code of 301. The $scheme variable is used to specify the protocol (HTTP or HTTPS), and the $request_uri variable is used to preserve the original URL path and query string.

Redirecting URLs Using Nginx Rewrite Rules

The final method to redirect non-WWW and WWW URLs with Nginx is by using rewrite rules. Nginx rewrite rules allow you to manipulate the URL and redirect it to a new location.

Here’s an example of how to redirect non-WWW URLs to WWW URLs using Nginx rewrite rules:

server {
    listen 80;
    server_name example.com;
    rewrite ^(.*)$ $scheme://www.example.com$1 permanent;
}

In this example, the rewrite directive is used to redirect the non-WWW URL, example.com, to the WWW URL, www.example.com. The ^(.*)$ pattern matches all URLs, and the $1 variable is used to preserve the original URL path. The permanent flag is used to indicate that the redirect is permanent.

Here’s an example of how to redirect WWW URLs to non-WWW URLs using Nginx rewrite rules:

server {
    listen 80;
    server_name www.example.com;
    rewrite ^(.*)$ $scheme://example.com$1 permanent;
}

In this example, the rewrite directive is used to redirect the WWW URL, www.example.com, to the non-WWW URL, example.com. The ^(.*)$ pattern matches all URLs, and the $1 variable is used to preserve the original URL path. The permanent flag is used to indicate that the redirect is permanent.

It’s important to note that Nginx rewrite rules are processed before other Nginx directives, such as server blocks, which can be used to redirect URLs even before the content of your website is served. This makes rewrite rules a powerful tool for URL redirection with Nginx.

Closing Thoughts

Redirecting non-WWW and WWW URLs with Nginx is important in improving your website’s SEO and user experience. By using server blocks, redirect directives, or rewrite rules, you can ensure that users reach the correct page on your website, even if they type in the wrong URL or if the URL structure has changed. In this article, we’ve discussed how to redirect non-WWW and WWW URLs with Nginx and provided several examples to help you get started.

Share to...