开发者

redirect to url when user type mysitename.com in asp.net

开发者 https://www.devze.com 2023-04-11 19:04 出处:网络
I have designed my website in asp.net and hosted it on web server. I want to redirect user to my site if us开发者_StackOverflow社区er will type mydimainName.com on http://www.mydimainName.com.

I have designed my website in asp.net and hosted it on web server. I want to redirect user to my site if us开发者_StackOverflow社区er will type mydimainName.com on http://www.mydimainName.com. If some one has solution then please let me know.

Thanks,

Munish


You'd want to redirect the user as soon as possible, so placing this in Application_BeginRequest of your Global.asax makes the most sense. You also likely want to do this as a 301 redirect for SEO purposes. Something like so is typically what I would do (not 100% tested)

protected void Application_BeginRequest(Object sender, EventArgs e)
{
     string sHost = Request.Url.Host.ToLower();

     if( sHost == "mydimainame.com" )
     {
          Response.StatusCode = 301;
          Response.Status = "301 Moved Permanently";
          Response.AddHeader( "Location", "http://www.mydimainName.com");
          Response.End();
     }
}


You can try something like this in your masterpage or Global.asax:

            string req = Request.Url.ToString();
            string a = req.Substring(0, 10).ToLower();

            if (a != "http://www")
            {

// this is for localhost and my staging at discountasp,

                if (a != "http://loc" && !req.Contains("discountasp"))
                {
                    Response.Status = "301 Moved Permanently";
                    Response.AddHeader("Location", "http://www." + req.Substring(7, req.Length - 7));
                    Response.End();
                }
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号