Solved

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

Posted on 2008-06-11
3
217 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

739 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