
When hosting a website, it's essential to ensure consistent URL structure for SEO benefits and user experience. A common practice is to redirect all traffic to either the www version (e.g., www.example.com) or the non-www version (e.g., example.com). You may run a website on the www or non-www address as a personal preference, but to prevent redundant hostnames it's recommended to choose one style and be consistent throughout your site. Whichever address you choose, you should make a permanent HTTP 301 redirect from the other address to prevent Google Analytics from logging it as a redundant hostname. This helps avoid duplicate content issues and provides a unified domain experience for your visitors.
This article explains how to redirect non-www and www URLs on Nginx by configuring it to redirect a non-www URL to the www address, or vice-versa.
Locate your virtual host configuration file, which is typically located in /etc/nginx/sites-available.
Add an new server block at the top, above the existing server block, like:
This directive tells Nginx to send any request for http://example.com to http://www.example.com with HTTP redirect code 301.
Restart Nginx to apply the change. Ubuntu or Debian systems use the command:
Locate your virtual host configuration file, which is typically located in /etc/nginx/sites-available.
Add an new server block at the top, above the existing server block, like:
This directive tells Nginx to send any request for http://www.example.com to http://example.com with HTTP redirect code 301.
Restart Nginx to apply the change. Ubuntu or Debian systems use the command:
If your Nginx server is hosting multiple domains, use this method to match all domain names.
To redirect the non-www URL to the www address for all domains, add this snippet inside the http directive in your nginx.conf file, which is usually located in /etc/nginx.
Or, to redirect the www URL to the non-www address for all domains, add this snippet inside the http directive in your nginx.conf file, which is usually located in /etc/nginx.
Restart Nginx to apply the change. Ubuntu or Debian systems use the command:
0 Comments
Be the first to comment and share your perspective with the community.