{"id":3699,"date":"2010-12-13T11:17:40","date_gmt":"2010-12-13T16:17:40","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/?p=3699"},"modified":"2010-12-13T11:17:40","modified_gmt":"2010-12-13T16:17:40","slug":"redirect-nginx-www","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/","title":{"rendered":"Redirect Nginx to www"},"content":{"rendered":"<p>From <a href=\"http:\/\/go2linux.garron.me\/linux\/2010\/11\/redirect-nginx-www-815\" target=\"_blank\" rel=\"noopener noreferrer\">go2linux.org<\/a><\/p>\n<p>Yesterday I\u2019ve written a post about how to redirect traffic from www.domain.com to domain.com using Nginx. It is using an if statement inside the server section in the <em>nginx.conf <\/em>file, there is another way to do it. It is to create two server section, and redirect the traffic from one of them to the other:<\/p>\n<p>Strip www from url with nginx redirect<br \/>\n<code><br \/>\nserver {<br \/>\n    server_name  www.domain.com;<br \/>\n    rewrite ^(.*) http:\/\/domain.com$1 permanent;<br \/>\n}<\/p>\n<p>server {<br \/>\n    server_name  domain.com;<br \/>\n    #The rest of your configuration goes here#<br \/>\n}<br \/>\n<\/code><\/p>\n<p>What you need to do, it use the same IP in your DNS server for both<\/p>\n<p>domain.com and www.domain.com, and when the browser asks for www.domain.com will be redirected to domain.com and will ask to the same server that info, in that case, it will get the proper answer from your server.<\/p>\n<p>Add the www to the url with nginx redirect<\/p>\n<p>If what you need is the opposite, to redirect from domain.com to www.domain.com, you can use this:<br \/>\n<code><br \/>\nserver {<br \/>\n    server_name  domain.com;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<\/p>\n<p>server {<br \/>\n    server_name  www.domain.com;<br \/>\n    #The rest of your configuration goes here#<br \/>\n}<br \/>\n<\/code><\/p>\n<p>As you can imagine, this is just the opposite and works the same way the first example.<\/p>\n<p>What I like about this, is that you can actually use this method to forward other domains you may own to an specific domain, let\u2019s see.<\/p>\n<p>Imagine you own:<\/p>\n<p>domain.com<br \/>\ndomain.net<br \/>\ndomain.org<\/p>\n<p>Your site is located at www.domain.com, you may write a configuration file like this:<br \/>\n<code><br \/>\nserver {<br \/>\n    server_name domain.org;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<br \/>\nserver {<br \/>\n    server_name www.domain.org;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<\/p>\n<p>server {<br \/>\n    server_name domain.net;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<br \/>\nserver {<br \/>\n    server_name www.domain.net;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<\/p>\n<p>server {<br \/>\n    server_name domain.com;<br \/>\n    rewrite ^(.*) http:\/\/www.domain.com$1 permanent;<br \/>\n}<br \/>\nserver {<br \/>\n    server_name www.domain.com;<br \/>\n    #From here you start your Nginx server normal configuration#<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Do not forget to manage your DNS server and on each zone assign the same IP for all<\/p>\n<p>domain.com<br \/>\nwww.domain.com<br \/>\ndomain.org<br \/>\nwww.domain.org<br \/>\ndomain.net<br \/>\nwww.domain.net<\/p>\n<p>From <a href=\"http:\/\/go2linux.garron.me\/linux\/2010\/11\/redirect-nginx-www-815\" target=\"_blank\" rel=\"noopener noreferrer\">go2linux.org<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From go2linux.org Yesterday I\u2019ve written a post about how to redirect traffic from www.domain.com to domain.com using Nginx. It is using an if statement inside the server section in the nginx.conf file, there is another way to do it. It is to create two server section, and redirect the traffic from one of them to&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-3699","post","type-post","status-publish","format-standard","hentry","category-info"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Redirect Nginx to www - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redirect Nginx to www - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"From go2linux.org Yesterday I\u2019ve written a post about how to redirect traffic from www.domain.com to domain.com using Nginx. It is using an if statement inside the server section in the nginx.conf file, there is another way to do it. It is to create two server section, and redirect the traffic from one of them to... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-13T16:17:40+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"Redirect Nginx to www\",\"datePublished\":\"2010-12-13T16:17:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/\"},\"wordCount\":268,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"General Info\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/\",\"name\":\"Redirect Nginx to www - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2010-12-13T16:17:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/redirect-nginx-www\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Redirect Nginx to www\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redirect Nginx to www - Linux Shtuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/","og_locale":"en_US","og_type":"article","og_title":"Redirect Nginx to www - Linux Shtuff","og_description":"From go2linux.org Yesterday I\u2019ve written a post about how to redirect traffic from www.domain.com to domain.com using Nginx. It is using an if statement inside the server section in the nginx.conf file, there is another way to do it. It is to create two server section, and redirect the traffic from one of them to... Read More","og_url":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2010-12-13T16:17:40+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"Redirect Nginx to www","datePublished":"2010-12-13T16:17:40+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/"},"wordCount":268,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["General Info"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/","url":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/","name":"Redirect Nginx to www - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2010-12-13T16:17:40+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/redirect-nginx-www\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"Redirect Nginx to www"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=3699"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3699\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=3699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=3699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=3699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}