Solved

301 Redirects not working for home page but works for all other pages

Posted on 2008-06-11
3
225 Views
Last Modified: 2010-04-20
I've created a script to run on my sites master page code behind file that redirects any non www. url to a www.url.  I did this because the bots were indexing http://site.com instead of http://www.site.com.  The problem lies in that the home page is not redirecting.  All the other pages are.

Here's what loads on Page_PreRender event

protected void Page_PreRender(object sender, EventArgs e)
    {
        string rawURL = Convert.ToString(Request.Url);
        string url = Convert.ToString(Request.Url);

        if (!url.Contains("www."))
        {
            string rURL = Convert.ToString(Request.RawUrl);
            string newURL = "http://www.site.com" + rURL;
            if (url.Contains("/home.aspx"))
            {
                newURL = "http://www.site.com/";
            }
            // do a 301 redirect
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.Status = "301 Moved Permanently";
            Response.StatusCode = 301;
            Response.AddHeader("Location", newURL);
            Response.Flush();

           


        }
    }
0
Comment
Question by:greeneggs614
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:squillman
ID: 21765276
Just to rule out the obvious, your home page is actually using the master page, yes?
0
 

Author Comment

by:greeneggs614
ID: 21766220
Good question.  Sometimes the most obvious items are the most overlooked.  Yes, I double checked, all pages on the site use the same masterpage.  Your question did bring to light the following piece of code
<%@ OutputCache Location="Server" Duration="60" VaryByParam="*" %>

Since I removed it the redirect does appear to be working.  The site was originally deigned by an outside firm so I don't know what exactly this code is used for.  It appears to cache the homepage for 60 seconds?  Can anybody provide more insight?
0
 
LVL 5

Accepted Solution

by:
squillman earned 250 total points
ID: 21769113
Yes, you are correct.  By removing that directive your page will get reloaded every time someone hits it instead of having it's cached output (which was precompiled without your redirect code) sent to the client.  This can be very good for a busy site, especially if you have any kind of dynamic content on the page.  You can get pretty granular on what ASP.NET caches.  Have a look at the following MSDN page if you are interested in learning more about ASP.NET caching:
http://msdn.microsoft.com/en-us/library/06bh14hk.aspx.  You might want to consider putting that directive back in now that your redirect code is being used.
0

Featured Post

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question