NGINX is a powerful and flexible web server that can be configured to perform various tasks, including URL redirection. Redirecting traffic from non-WWW to WWW or vice versa is a common requirement to ensure consistent access to your website, improve SEO, and enhance user experience. This setup ensures that all visitors are directed to a single version of your site, which helps with search engine indexing and eliminates duplicate content issues.
This guide will demonstrate how to configure NGINX to redirect non-WWW to WWW and WWW to non-WWW, providing clear steps and examples to achieve this setup efficiently.
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 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
By configuring NGINX to redirect non-WWW to WWW or vice versa, you ensure consistent access to your website and improve SEO. Regularly check your NGINX configuration to ensure it works as expected and make adjustments if necessary. Implementing these redirections helps maintain a unified online presence and enhances the user experience by directing all traffic to a single version of your site.