{"id":1187,"date":"2017-03-11T16:17:10","date_gmt":"2017-03-11T10:47:10","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1187"},"modified":"2026-09-28T20:02:40","modified_gmt":"2026-09-28T14:32:40","slug":"redirect-non-www-request-www-nginx","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/redirect-non-www-request-www-nginx\/","title":{"rendered":"How to Redirect Non-WWW Requests to WWW Using Nginx"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date<\/strong>: March 3, 2017<br><strong>Last Updated<\/strong>: September 28, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, we will explain how to configure Nginx to redirect all non-www requests to the www version of a domain. This configuration ensures that visitors accessing the website without the <code>www<\/code> prefix are automatically redirected to the corresponding <code>www<\/code> URL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before implementing this configuration, ensure that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nginx is installed and running on the server.<\/li>\n\n\n\n<li>You have root or sudo access to the server.<\/li>\n\n\n\n<li>A valid domain name is configured and pointing to the server.<\/li>\n\n\n\n<li>You have access to the Nginx configuration file located at <code>\/etc\/nginx\/sites-enabled\/default<\/code>.<\/li>\n\n\n\n<li>If Nginx is not installed, refer to the installation guides for Ubuntu 16.04 or CentOS 7 mentioned in this post.<\/li>\n<\/ul>\n\n\n<h2>Implementation<\/h2>\n<p>The default Nginx configuration file is <em>\/etc\/nginx\/sites-enabled\/default.<\/em><\/p>\n<p>Open the Nginx configuration and add the following lines.<\/p>\n<p><code>vi \/etc\/nginx\/sites-enabled\/default<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">server {\n    listen 80;\n    server_name domain.tld;\n\n    return 301 http:\/\/www.domain.tld$request_uri;\n}<\/pre>\n<p>Check for syntax errors<\/p>\n<p><code>nginx -t<\/code><\/p>\n<p>If there are no errors, restart the nginx service to make the changes take effect.<\/p>\n<p><code>\/etc\/init.d\/nginx restart<\/code><\/p>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By adding the appropriate server block to the Nginx configuration, all non-www requests can be automatically redirected to the www version of the domain. After updating the configuration, use <code>nginx -t<\/code> to verify the syntax and restart Nginx to apply the changes. This provides a consistent URL format for accessing the website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Why should I redirect non-www requests to www?<\/strong><br>Redirecting non-www requests to www ensures that visitors consistently access the website using a single URL format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Where is the default Nginx configuration file located?<\/strong><br>On Ubuntu systems, the default Nginx configuration is generally located at <code>\/etc\/nginx\/sites-enabled\/default<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. How can I check whether the Nginx configuration has any errors?<\/strong><br>Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -t<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration is correct, Nginx will report that the syntax is OK.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/redirect-http-https-nginx-behind-load-balancers\/\">Redirect http to https on nginx behind load balancers<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business?. Our experts can help with\u00a0web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team\u00a0<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: March 3, 2017Last Updated: September 28, 2026 Introduction In this post, we will explain how to configure Nginx [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[161,271,274],"class_list":["post-1187","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-centos","tag-nginx","tag-ubuntu","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-j9","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1187"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":11977,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1187\/revisions\/11977"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}