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?
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.
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.
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.
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With