Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global 301 redirection from domain to www.domain

could i use the begin request of Global.asax to redirect everything,

from mydomain.domain to www.mydomain.domain?

If this one is true, how can i do that?

like image 535
OrElse Avatar asked Jan 21 '10 18:01

OrElse


People also ask

How do I redirect a website to www?

Under Type, select the Permanent (301) option. On https?://, enter the domain you want to redirect. Leave the path section (/) empty. In the Redirects to field, type in your website's www URL.

Can I 301 redirect from one domain to another?

A 301 redirect is a permanent redirect from one URL to another. While they can redirect site page URLs, they can also redirect from one domain to another.

How do I redirect www to non www in DNS?

You can direct www to non-www by adding a new server block to your nginx configuration file. Step 1: Add the following server block to your nginx configuration file. Step 2: Restart nginx.


2 Answers

A couple of minor changes to Jan's answer got it working for me:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower(); 
    if (currentUrl.StartsWith("http://mydomain"))
    {
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
        Response.End();
    }
}

Changes were to use the BeginRequest event and to set currentUrl to HttpContext.Current.Request.Url instead of HttpContext.Current.Request.Path. See:

http://www.mycsharpcorner.com/Post.aspx?postID=40

like image 83
Tom Wayson Avatar answered Nov 15 '22 07:11

Tom Wayson


protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
  string currentUrl = HttpContext.Current.Request.Path.ToLower();
  if(currentUrl.StartsWith("http://mydomain"))
  {
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
    Response.End();
  }
}
like image 25
Jan Jongboom Avatar answered Nov 15 '22 05:11

Jan Jongboom