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.

Conclusion

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.

Frequently Asked Questions

Q: Why is it important to redirect non-WWW and WWW URLs with Nginx?

A: Redirecting non-WWW and WWW URLs with Nginx helps to improve your website’s SEO and user experience. It ensures that users reach the correct page on your website even if they type in the wrong URL or if the URL structure has changed.

Q: What are the different types of URL redirects?

A: The different types of URL redirects include 301 redirects, 302 redirects, and 303 redirects.

Q: How can I redirect non-WWW and WWW URLs with Nginx?

A: You can redirect non-WWW and WWW URLs with Nginx by using server blocks, redirect directives, or rewrite rules.

Q: What happens if I don’t redirect non-WWW and WWW URLs with Nginx?

A: If you don’t redirect non-WWW and WWW URLs with Nginx, users may reach different pages on your website depending on whether they type in the non-WWW or WWW URL. This can result in duplicate content and hurt your website’s SEO. Additionally, it can create a poor user experience if users reach a broken or incorrect page.

Q: Can I redirect non-WWW and WWW URLs to a different domain with Nginx?

A: Yes, you can redirect non-WWW and WWW URLs to a different domain with Nginx by modifying the return or rewrite directive to specify the target domain.

Q: Can I redirect multiple URLs with Nginx?

A: Yes, you can redirect multiple URLs with Nginx by using multiple server blocks, redirect directives, or rewrite rules.

Q: How do I know if my URL redirects are working correctly with Nginx?

A: You can use online tools such as the Redirect Checker tool to verify that your URL redirects are working correctly with Nginx. Simply enter your original URL and the expected redirected URL, and the tool will test the redirect and indicate whether it’s working correctly.

Q: Can I redirect non-WWW and WWW URLs with other web servers besides Nginx?

A: Yes, you can redirect non-WWW and WWW URLs with other web servers besides Nginx, such as Apache. The specific method of redirecting URLs will depend on your web server.

Q: What is the $scheme:// in the Nginx?

A: $scheme:// in the Nginx configuration of redirecting is a variable that specifies the protocol (HTTP or HTTPS) for the URL being redirected. It’s used in conjunction with the return or rewrite directive to create a complete URL, including the scheme (HTTP or HTTPS), for the target location of the redirect. This ensures that the correct protocol is used for the redirected URL, which can be important for preserving the security of your website and ensuring that users are redirected to the correct location.

Share to...