Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

Question has a verified solution.

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

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…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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.

670 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