Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

Redirect non-www to www domain in asp.net

In ASP.NET MVC, you can redirect all HTTP requests from non-www to "www" version of your domain/website in two ways:

  1. Using the web.config
  2. Using global.asax

Redirect from non-www to www using the web.config

You can add rules for redirecting or rewriting HTTP requests at the IIS level in the web.config. This is the best place to redirect or rewrite URLs for your website.

Add the redirect rules in the <rewrite><rewrite> section of <system.webServer> section in the web.config.

Example: Redirect Rule in web.config
void Application_BeginRequest(object sender, EventArgs e)
{
    if (!Context.Request.Url.Authority.StartsWith("www") && !Context.Request.IsLocal)            
    {
        var url = string.Format("https://www.{0}{1}",
                        Context.Request.Url.Authority,
                        Context.Request.Url.PathAndQuery);
            
        Response.RedirectPermanent(url, true);
    }
}

Visit URL rewrite module for more information.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.