What is the most recommended way to redirect 301 to asp.net?

I went googled all over and I read a ton of different answers - I tried to use some of them without success. I am not a professional programmer, but I thank the people on this forum for teaching so much about everything from jquery to doctypes to you to call it!

I have a website developed in VWD 2010 Express. I just want to know 2 things:

  • I know that it’s bad for search engines / duplicate content to have yourdomain and www.yourdomain, so I want to set up 301 redirects so that if the end user types in mydomain, they are automatically redirected to www.mydomain (from www).

  • I moved several pages that are not in the root, but in folders. Therefore, I want to replace the outdated page with a new location. I want to do this by automatically redirecting them from www.mydomain / services /engineering.aspx to www.mydomain / products /engineering.aspx.

It's complicated? (recommended) to use .htaccess or web.config or something else?

Thank you for reading this, and I sincerely appreciate any feedback.

Jason weber

+6
source share
3 answers

If I did not understand .htacess for Apache, and if you are coding in ASP.Net, you are almost certainly using IIS. This way you can use the .htaccess material in your research.

You can use some kind of Rewriter URL for your redirects, but it can get very complicated. If I were you, I would set it as simple as possible and execute your old 301 page redirects in the Page_Load event ie

protected void Page_Load(object sender, System.EventArgs e) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location","http://www.domainname.com/new-page.aspx"); } 

For your canonical redirect (not from www to www) you can do something like this in the Global.asax file in Application_BeginRequest to detect a non www variant.

 if (HttpContext.Current.Request.Url.ToString().ToLower().Contains( "http://mysite.com")) { HttpContext.Current.Response.Status = "301 Moved Permanently"; HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace( "http://mysite.com", "http://www.mysite.com")); } 

(this is not my code derived from here )

This is what I would like to do anyway - it is useful in order to be clear and to keep you away from any strange configuration of the web server, which I sometimes find in the black box.

+7
source

Since you are using VWD 2010 Express, it will be useful for you to use the .NET4 Response.RedirectPermanent (stringURL) method. It is part of the HttpResponse object.

Additional documentation is available here: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.redirectpermanent.aspx

These functions are similar to Response.Redirect (), however, it issues the 301 code to the client along with the redirect.

+6
source

Do this if you want to redirect through 301

 string url = "newpage.aspx"; Response.Clear(); Response.StatusCode = 301; Response.RedirectLocation = url.ToLower(); Response.End(); 

Then use the verification tool from Chrome and go to the "Network" tab to see the status code. This works for me.

-1
source

Source: https://habr.com/ru/post/905416/


All Articles