Page 1 of 2

<title> and <meta> tags - again

Posted: Wed Nov 26, 2008 11:06 am
by draneb
Hello,

Since this is a serious issue for me and it went unanswered last time I would like to ask again.

I, with the help of Judy, worked very hard to get my AC 5.5 ranked very, very well for all of my search terms in Google and Yahoo. If there was a product I carried and I typed some of those words into Google I was always on the first page, most of the time 1, 2 or 3.

When I switched to AC 7 my listings completely disappeared. Judy incorporated the same title and meta information (using custom programming) that was used on my 5.5 site. Google and Yahoo are living on my AC 7 site, daily, but the only listings I can find are ones that are from the 5.5 pages that haven't been re-indexed.

Anyways, for whatever reason I dropped, I still need to find a way to list the <title> and <meta> tags in their traditional way. Look at these tags from the AC 7 demo site, this is just sloppy.

<link href="App_Themes/Glass_SilverAge/ComponentArt.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/print.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/style.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/webparts.css" type="text/css" rel="stylesheet" /><title>
Home Page
</title><style type="text/css">

Everything just runs together. There are no line breaks between tags and why does the title tag display with the title on a separate line with tab spacing in front of it?

I knew I should have waited to after Christmas to go live with the new site. This isn't going to be good.
Can this be fixed? Is it buried in compiled code somewhere?

Re: <title> and <meta> tags - again

Posted: Wed Nov 26, 2008 12:49 pm
by sohaib
Anyways, for whatever reason I dropped, I still need to find a way to list the <title> and <meta> tags in their traditional way. Look at these tags from the AC 7 demo site, this is just sloppy.

<link href="App_Themes/Glass_SilverAge/ComponentArt.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/print.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/style.css" type="text/css" rel="stylesheet" /><link href="App_Themes/Glass_SilverAge/webparts.css" type="text/css" rel="stylesheet" /><title>
Home Page
</title><style type="text/css">

Everything just runs together. There are no line breaks between tags and why does the title tag display with the title on a separate line with tab spacing in front of it?
I am not expert on search engine optimization but I see no reason why the tags being output like the above should make any difference to a search engine bot which is a program. Search engine bots are parsing the tags not the lines and in HTML white space (including new line) has no significance.

There are hundreds of factors affecting search engine ranking. I am sure there are other factors on your new AC7 store affecting your ranking. I am unable to convince myself that absence of line breaks or other 'formatting' could be the reason.... but who knows.

Google's ranking formula is certainly complicated. It might be taking into account your old pages and new pages at the same time ... resulting in a lower score.

Re: <title> and <meta> tags - again

Posted: Wed Nov 26, 2008 1:49 pm
by draneb
Yes, I know. I have operated a website for over 10 years and trying to figure them out is almost impossible but I did have a good system going.

You are correct. Having duplicate pages from 5.5 to 7.0 most likely got me flushed down the toilet. I have product pages ending in PxxxCxxx.aspx and now the same pages ending only in Pxxx.aspx.

Can't you program something in to make those pages with the Cxxx on the end show up as 404 errors? I was able to get 404 errors just by changing the C to a lowercase c. Then maybe Google can index the site correctly. That is another serious issue (customers who used previous versions of AC)

I wasn't blaming my drop on the meta tags and title layout, but "why not?" have them shown that way? It must be something too difficult because it otherwise just doesn't make any sense. It definitely wouldn't hurt...

Thank you.

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 2:04 am
by sohaib
I wasn't blaming my drop on the meta tags and title layout, but "why not?" have them shown that way? It must be something too difficult because it otherwise just doesn't make any sense. It definitely wouldn't hurt...
I agree. Mazhar will have a look at this and provide a workaround if possible.
What happens is the for the purpose of optimizing the page size, quite often, the rendering code strips out empty spaces from HTML. We will check if that is the case...

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 2:34 am
by TTMedia
The problem with upgrading your ecom site, especially if your old site is dynamic is that you open up room for duplicate content penalty.

A 404 error will not help you. You need to perform a 301 error, (permanent redirect). Now the problem is that your old site is all dynamic content, and with IIS, you can't do a 301 redirect with no dynamic content.. unless you are using a url re-write program from the get go and can recreate the file/folder structure.

Also, when you update your site so drastically but change the complete code, Google will raise red flags and proceed with caution on your site. It will take a while for everything to get back together.. but it sure is a pain in the a$$

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 5:33 am
by draneb
Thanks Sohaib. That would be great.

Thanks TTMedia. You are right, a 301 redirect would have to be the way to go. So search engines won't remove the page if they keep getting a 404 error?

I have a file on the server that sends me an email with any errors. Slurp and Googlebot are just living on the site, daily, so that is good. I just have to try and get rid of the duplicate content to get back on good terms with them. :roll:

Hapy Thanksgiving!

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 8:22 am
by jmestep
I'm checking on this also. I've done some meta stuff on Draneb's site, but since I'm working full time for Web2Market, I haven't had a lot of time to help him. I remember in some places putting a /n in code to generate a new line.

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 9:00 am
by mazhar
Regarding 301 Redirect Pages
The solution for 301 redirect pages has been already discussed in some threads. Have a look at the following threads
viewtopic.php?f=42&t=7707
viewtopic.php?f=42&t=8004

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 12:47 pm
by mazhar
Here is the code to put proper line breaks in the generated HTML. You need to put this method in each page for which you want to have line breaks in the head.

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                int headStartIndex = pageHtml.IndexOf("<head");
                int headEndIndex = pageHtml.IndexOf("</head>");
                string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                StringBuilder sb = new StringBuilder(headerPart);
                sb.Replace("<link", "\r<link");
                sb.Replace("<META", "\r<META");
                sb.Replace("<style", "\r<style");

                int sindex = headerPart.IndexOf("<title>");
                int eindex = headerPart.IndexOf("</title>");
                string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                titleText = titleText.Replace("\r", string.Empty);
                titleText = titleText.Replace("\t", string.Empty);
                titleText = titleText.Replace("\n", string.Empty);
                titleText.Trim();
                titleText = String.Format("\r<title>{0}</title>", titleText);
                sb.Replace(titleTag, titleText);
                sb.Replace("</head>", "\r</head>");
                string newHeaderPart = sb.ToString();
                pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);                    
    }
OUT PUT
<link href="App_Themes/AbleCommerce/ComponentArt.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/AbleCommerce/Gallery.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/AbleCommerce/print.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/AbleCommerce/style.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/AbleCommerce/webparts.css" type="text/css" rel="stylesheet" />
<title>Home Page</title>
<style type="text/css">
.ctl00_wpm_HomePage_ctl02_CategoryTree_0 { text-decoration:none; }
.ctl00_ContentZone_0 { border-color:Black;border-width:1px;border-style:Solid; }

</style>
</head>

Re: <title> and <meta> tags - again

Posted: Thu Nov 27, 2008 3:15 pm
by draneb
Thank you Mazhar!
Thank you Sohaib!

You are awesome. It works great.

Re: <title> and <meta> tags - again

Posted: Fri Nov 28, 2008 6:45 am
by draneb
Mazhar, I put the code in my Product.aspx file and the tags were displaying great.

Just testing around, as I quite often do, I found that if I click on my Write a Review link I get redirected to my custom 404 error page. If I take the code out I can click on the Write a Review link and get the form without any problems.

Would you happen to be able to verify this on yours?

Thank you.

Re: <title> and <meta> tags - again

Posted: Fri Nov 28, 2008 6:46 am
by mazhar
let me check

Re: <title> and <meta> tags - again

Posted: Fri Nov 28, 2008 7:00 am
by mazhar
I think this problem is due to ajax call. We need to make sure that if the response html contains head tag then we need to adjust it otherwise do nothing. Try the following updated code

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>", titleText);
                    sb.Replace(titleTag, titleText);
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }

Re: <title> and <meta> tags - again

Posted: Fri Nov 28, 2008 10:27 am
by draneb
Hi Mazhar,

That code worked. Thank you very much.

Re: <title> and <meta> tags - again

Posted: Fri Nov 28, 2008 1:25 pm
by jmestep
One more tweak would be nice:
Render the <title> tags before the other <meta> tags. SEO software complains that it should be first. I've experimented some, but other things in the page code seem to override what line I put the Page.Title on.

Re: <title> and <meta> tags - again

Posted: Sat Nov 29, 2008 1:37 am
by mazhar
One more tweak would be nice:
Render the <title> tags before the other <meta> tags. SEO software complains that it should be first. I've experimented some, but other things in the page code seem to override what line I put the Page.Title on.
I think it could be done by interchanging the title tag with first meta tag.

Re: <title> and <meta> tags - again

Posted: Sat Nov 29, 2008 8:33 am
by jmestep
I've tried moving it up above the meta tags in the above code and it didn't change. I'll try again.

Re: <title> and <meta> tags - again

Posted: Sat Nov 29, 2008 3:07 pm
by jmestep
Is there a way this could be put into the pagehelper.cs? I tried it and got an error
CS0115: 'PageHelper.Render(System.Web.UI.HtmlTextWriter)': no suitable method found to override

Re: <title> and <meta> tags - again

Posted: Mon Dec 01, 2008 6:09 am
by mazhar
I've tried moving it up above the meta tags in the above code and it didn't change. I'll try again.
From interchange i mean that we need to interchange the meta tags and tittle tag information in available page HTML string not by interchanging code statements. I have tried to put this functionality as well, the newly added code contains the comments to describe the purpose of statements. Please try the code

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>", titleText);
                    sb.Replace(titleTag, titleText);
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    
                    //find out the index of first meta tag
                    int metaIndex = newHeaderPart.IndexOf("<META", StringComparison.CurrentCultureIgnoreCase);
                    
                    //meta tag found
                    if (metaIndex >= 0)
                    {
                        //remove the current title information
                        newHeaderPart.Replace(titleText, string.Empty);
                        
                        //inject the title information at first meta tag index
                        newHeaderPart = newHeaderPart.Insert(metaIndex, titleText);
                    }
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }

Re: <title> and <meta> tags - again

Posted: Mon Dec 01, 2008 6:23 am
by mazhar
Is there a way this could be put into the pagehelper.cs? I tried it and got an error
CS0115: 'PageHelper.Render(System.Web.UI.HtmlTextWriter)': no suitable method found to override
You can not move the complete Render method inside the PageHelper. Its something specific to the ASPX Page. I am overriding the ASPX default rendering by providing an overridden version of Render method in each page. A part of method could be moved inside the PageHelperbut still you will need it in each page.

For example in PageHelper you can create the following function.

Code: Select all

public static void AdjustHTML(ref string pageHtml) 
    {
        if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
        {
            int headStartIndex = pageHtml.IndexOf("<head");
            int headEndIndex = pageHtml.IndexOf("</head>");

            string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

            StringBuilder sb = new StringBuilder(headerPart);
            sb.Replace("<link", "\r<link");
            sb.Replace("<META", "\r<META");
            sb.Replace("<style", "\r<style");

            int sindex = headerPart.IndexOf("<title>");
            int eindex = headerPart.IndexOf("</title>");
            string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
            string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
            titleText = titleText.Replace("\r", string.Empty);
            titleText = titleText.Replace("\t", string.Empty);
            titleText = titleText.Replace("\n", string.Empty);
            titleText.Trim();
            titleText = String.Format("\r<title>{0}</title>", titleText);
            sb.Replace(titleTag, titleText);
            sb.Replace("</head>", "\r</head>");
            string newHeaderPart = sb.ToString();

            //find out the index of first meta tag
            int metaIndex = newHeaderPart.IndexOf("<META", StringComparison.CurrentCultureIgnoreCase);

            //meta tag found
            if (metaIndex >= 0)
            {
                //remove the current title information
                newHeaderPart.Replace(titleText, string.Empty);

                //inject the title information at first meta tag index
                newHeaderPart = newHeaderPart.Insert(metaIndex, titleText);
            }
            pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
        }
    }

and in pages you can use the following version of the Render method

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();
                PageHelper.AdjustHTML(ref pageHtml);                
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }

Re: <title> and <meta> tags - again

Posted: Mon Dec 01, 2008 7:55 am
by jmestep
Thank you, mazhar. I will work on the title tag. On moving it all to Pagehelper.cs, I was just looking for a way to have the code in only one place, but I understand now why I can't.

Re: <title> and <meta> tags - again

Posted: Fri Dec 19, 2008 1:46 pm
by krissato
Thank you for posting this code! I did some fine-tuning - the casing on "META" didn't match what I had, and the <title> in its original position was not getting removed. This is the modified code that works for me:

protected override void Render(HtmlTextWriter writer)
{
string pageHtml = string.Empty;

using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
base.Render(hw);
pageHtml = sw.ToString();

if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
{
int headStartIndex = pageHtml.IndexOf("<head");
int headEndIndex = pageHtml.IndexOf("</head>");

string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

StringBuilder sb = new StringBuilder(headerPart);
sb.Replace("<link", "\r<link");
sb.Replace("<Meta", "\r<Meta");
sb.Replace("<style", "\r<style");

int sindex = headerPart.IndexOf("<title>");
int eindex = headerPart.IndexOf("</title>");
string titleTag = headerPart.Substring(sindex, (eindex + 8 ) - sindex);
string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
titleText = titleText.Replace("\r", string.Empty);
titleText = titleText.Replace("\t", string.Empty);
titleText = titleText.Replace("\n", string.Empty);
titleText.Trim();
titleText = String.Format("\r<title>{0}</title>", titleText);
sb.Replace(titleTag, titleText);
sb.Replace("</head>", "\r</head>");
string newHeaderPart = sb.ToString();

//find out the index of first meta tag
int metaIndex = newHeaderPart.IndexOf("<Meta", StringComparison.CurrentCultureIgnoreCase);

//meta tag found
if (metaIndex >= 0)
{
//remove the current title information
string updatedHeaderPart = newHeaderPart.Replace(titleText, string.Empty);

//inject the title information at first meta tag index
newHeaderPart = updatedHeaderPart.Insert(metaIndex-1, titleText);
}
pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
}
hw.Close();
}
sw.Close();
}
writer.Write(pageHtml);
}

Re: <title> and <meta> tags - again

Posted: Sat Dec 20, 2008 5:39 am
by mazhar
Thanks for sharing the code krissato :D

Re: <title> and <meta> tags - again

Posted: Mon Feb 16, 2009 7:54 pm
by Brewhaus
Were exactly should this code be placed?

Re: <title> and <meta> tags - again

Posted: Tue Feb 17, 2009 4:36 am
by mazhar
In all root pages of website for example Default.aspx, ContactUs.aspx etc.