Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now