Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-11
3
Medium Priority
?
233 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
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…
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…
Suggested Courses

876 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