{"id":885,"date":"2018-06-01T08:48:20","date_gmt":"2018-06-01T08:48:20","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=885"},"modified":"2026-04-02T09:55:15","modified_gmt":"2026-04-02T09:55:15","slug":"seo-tip-set-preferred-url-in-asp-net-application","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/seo-tip-set-preferred-url-in-asp-net-application\/","title":{"rendered":"SEO Tip: Set Preferred URL in ASP.NET Application"},"content":{"rendered":"<p>Search engines treat website URL with and without &#8220;www&#8221; differently. Though both URLs point to the same destination, it&#8217;s important to pick one and set as your preferred URL so that search engines don&#8217;t\u00a0two URLs as duplicate content. Websites use 301 redirect for this.<\/p>\n<p>The example code to redirect non-www URL to www URL\u00a0in an ASP.NET application would be as following. Add following code snippet in Application_BeginRequest method of your Global.asax. Replace URL (somewebsite.com) with your website URL in the code and you are all set.<\/p>\n<pre class=\"lang:default decode:true\">protected void Application_BeginRequest(object sender, EventArgs e)\r\n{\r\n    if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith(\"http:\/\/somewebsite.com\"))\r\n    {\r\n        string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace(\"http:\/\/somewebsite.com\", \"http:\/\/www.somewebsite.com\");\r\n        Response.Status = \"301 Moved Permanently\";\r\n        Response.AddHeader(\"Location\", newUrl);\r\n    }\r\n}<\/pre>\n<p>If you want to do the opposite and direct www URL to non-www URL simply change the code as following<\/p>\n<pre class=\"lang:default decode:true \">protected void Application_BeginRequest(object sender, EventArgs e)\r\n{\r\n    if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith(\"http:\/\/www.somewebsite.com\"))\r\n    {\r\n        string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace(\"http:\/\/www.somewebsite.com\", \"http:\/\/somewebsite.com\");\r\n        Response.Status = \"301 Moved Permanently\";\r\n        Response.AddHeader(\"Location\", newUrl);\r\n    }\r\n}<\/pre>\n<p>It does not make a different to search engines which one (www or non-ww) do you prefer but you do need to pick one of the two URLs as your preferred URL to improve SEO ranking of your site.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search engines treat website URL with and without &#8220;www&#8221; differently. Though both URLs point to the same destination, it&#8217;s important to pick one and set as your preferred URL so that search engines don&#8217;t\u00a0two URLs as duplicate content. Websites use 301 redirect for this. The example code to redirect non-www URL to www URL\u00a0in an ASP.NET application would be as following. Add following code snippet in Application_BeginRequest method of your Global.asax. Replace URL (somewebsite.com) with your website URL in the code and you are all set. protected void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith(&#8220;http:\/\/somewebsite.com&#8221;)) { string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace(&#8220;http:\/\/somewebsite.com&#8221;, &#8220;http:\/\/www.somewebsite.com&#8221;); Response.Status = &#8220;301 Moved Permanently&#8221;; Response.AddHeader(&#8220;Location&#8221;, newUrl); } } If you want to do the opposite and direct www URL to non-www URL simply change the code as following protected void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith(&#8220;http:\/\/www.somewebsite.com&#8221;)) { string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace(&#8220;http:\/\/www.somewebsite.com&#8221;, &#8220;http:\/\/somewebsite.com&#8221;); Response.Status = &#8220;301 Moved Permanently&#8221;;[&#8230;]<\/p>\n","protected":false},"author":5,"featured_media":1540,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,14],"tags":[],"class_list":["post-885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-how-to"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=885"}],"version-history":[{"count":2,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/885\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/885\/revisions\/888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/1540"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}