How to Redirect Non-WWW or WWW in Nginx

This guide will show you how to redirect Non-WWW to WWW or vice versa in Nginx, with clear examples of Nginx configurations.

Redirecting Non-WWW to WWW URLs in Nginx is an essential task for website administrators. It’s not just about ensuring that your website is accessible with or without the ‘www’ prefix, but also about maintaining consistent user experiences and search engine optimization. In this guide, we’ll walk through the process step-by-step, providing practical examples to make implementation easy.

Key points to consider:

  • Ease of Access: Helps users reach your site, regardless of how they type the URL.
  • Better SEO: Prevents duplicate content, improving your site’s search engine ranking.
  • Consistent User Experience: Offers a uniform URL structure for visitors.
  • Streamlined Site Management: Combines traffic sources for easier website management.

Next, we’ll dive into the how-to part, focusing on the setup and understanding of each step for effective URL redirection in Nginx.

Understanding NGINX URL Redirects

Before exploring the implementation of WWW to Non-WWW redirects (and vice versa) in NGINX, it’s essential to grasp the basic concepts of URL redirections. Understanding these fundamentals ensures effective and SEO-friendly website management.

Understanding NGINX URL Redirects: Key Types

  • 301 Redirects:
    • Purpose: Indicate a permanent URL change.
    • Use Case: Ideal when a web page has moved permanently to a new location.
    • SEO Impact: Transfers SEO rankings from the old URL to the new one, preserving search engine credibility.
  • 302 Redirects:
    • Purpose: Signify a temporary URL change.
    • Use Case: Useful during site maintenance or temporary content shifts, signaling a future return to the original URL.
    • SEO Impact: Tells search engines to keep the original URL indexed, as the change is not permanent.
  • 303 Redirects:
    • Purpose: Manage form submissions by preventing data re-submission on page refresh.
    • Use Case: Primarily employed in situations involving form submission confirmations.
    • User Experience: Enhances user experience by preventing duplicate form submissions and potential data errors.

Redirect Non-WWW to WWW URL in Nginx

Redirect with Nginx Server Blocks

Nginx server blocks are effective for managing redirections. Here’s how you can redirect from a Non-WWW to a WWW URL.

Configuration Example:

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

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

This configuration redirects requests from example.com (Non-WWW) to www.example.com (WWW). The $scheme and $request_uri preserve the protocol and path.

Nginx Redirect Directives

Redirect directives offer a straightforward way to implement redirections.

Directive Example:

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

This directive redirects example.com (Non-WWW) to www.example.com (WWW), maintaining the original URL path and protocol.

Redirect WWW to Non-WWW URL in Nginx

Utilizing Nginx Server Blocks for WWW to Non-WWW

Here’s how to set up a server block for redirecting from WWW to Non-WWW URLs.

Configuration Example:

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

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

This setup redirects www.example.com to example.com, using $scheme and $request_uri for a seamless transition.

Implementing Redirects with Nginx Rewrite Rules

Rewrite rules in Nginx are a powerful way to handle URL manipulations.

Rewrite Rule Example:

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

This rule changes www.example.com to example.com. The ^(.*)$ pattern captures all URLs, appending them to example.com ($1), and marks the redirect as permanent.

Closing Thoughts

In this guide, we’ve navigated the practicalities of redirecting URLs in Nginx, covering both Non-WWW to WWW and WWW to Non-WWW scenarios. We delved into using server blocks, redirect directives, and rewrite rules, each method offering its unique approach to efficient URL management. Remember, choosing the right redirection method is crucial for your site’s accessibility and SEO performance. So, take a moment to review your configurations, ensuring they align with your website’s needs.

Leave a Comment


Your Mastodon Instance
Share to...