6

I've googled everything, and I've read a ton of different responses -- some I've tried to implicate with no success. I'm not a professional programmer, but I thank people in this forum for teaching me so much about everything, from jquery to doctypes, to you name it!

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

  1. I know it's bad for search engines/duplicate content to have yourdomain and www.yourdomain both, so I want to set up a 301 redirect so that if an end-user types in mydomain, they're automatically redirected to www.mydomain (with the www).

  2. I've moved some pages that aren't in the root, but in folders. So I want to replace the outdated page with the new location. I want to do this by automatically redirecting them from www.mydomain/services/engineering.aspx to www.mydomain/products/engineering.aspx.

Is this difficult? Is it (recommended) to use .htaccess, or web.config, or something else?

Thank you for your time in reading this, and I sincerely appreciate any and all feedback.

Jason Weber

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jason Weber
  • 5,653
  • 6
  • 40
  • 73

3 Answers3

8

Unless I've misunderstood .htacess is for Apache and if you are coding in ASP.Net you are almost certainly using IIS. So igonore the .htaccess stuff in your research.

You could use some kind of URL Rewriter for your redirects but that can get very complex. If i were you I would keep it as simple as possible and do your old page 301 redirects in the Page_Load event i.e.

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 (non www to www) you could do similar in the Global.asax file in Application_BeginRequest to detect the non www variant i.e.

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 isn't my code it came from here)

This is what I would do anyway - it has the benefit of being easy to understand and keep you out of the way of any strange web server config which I find a bit of a black box sometimes.

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
  • Excellent information, Crab -- much appreciated! I feel pretty dumb for not recognizing the .htaccess/apache stuff, since I'm on a windows server. The information and link are excellent -- perfect advice for my situation. These redirects shouldn't be too difficult to implement. Thanks again for your time! – Jason Weber Jan 07 '12 at 20:50
  • @JasonWeber No problem at all. Good luck with it all – Crab Bucket Jan 07 '12 at 21:17
  • @CrabBucket would it cause any problem if we placed this code on page PreLoad instead of page Load? Would that affect URLs shared on social media (open graph)? – Rami Zebian Jan 12 '19 at 21:35
6

Since you are using VWD 2010 Express, you would benefit from using the .NET4 method Response.RedirectPermanent(stringURL). It is part of the HttpResponse object.

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

This functions that same was as Response.Redirect() however it issues a 301 code to the client along with performing the redirect.

Kyle B.
  • 5,737
  • 6
  • 39
  • 57
-1

Do this where you want to redirect over 301

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

Then use the inspect tool from Chrome and check on "Network" tab to see the Status Code. It works for me.