homepage Welcome to WebmasterWorld Guest from 54.225.24.227
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Head canonical tag locking site
problems with canonical tag
rp024




msg:4541566
 7:32 pm on Feb 1, 2013 (gmt 0)

Hello, I have a question about adding the canonical tag to the page head.

I have always used this

<link rel="canonical" href="http://www.mysite.com"/>

Problem is the site I'm trying to add this too now utilizes a single header called from <includes/header> tag in the page config.

How do you add the canonical to the index when it's a shared header? I tried adding it but the page goes blank.

 

rp024




msg:4541568
 7:33 pm on Feb 1, 2013 (gmt 0)

Sorry, thank you for any assistance in advance. It's greatly appreciated.

tedster




msg:4541917
 4:35 am on Feb 3, 2013 (gmt 0)

I don't know the PHP code (sorry) but I do want to point out that the canonical tag you traditionally used is an error.

The href attribute should NOT be the domain name in every case. Instead is should be the on intended URL for the actual content of that unique page. The only page that would use the canonical tag as you've written it would be the home page.

incrediBILL




msg:4541919
 4:58 am on Feb 3, 2013 (gmt 0)

You can embed PHP variables in HTML like this:

<link rel="canonical" href="<?=$url?>"/>

Meaning you can put any href in the variable $url as long as the variable is created in PHP before the header is included.

rp024




msg:4542271
 3:52 pm on Feb 4, 2013 (gmt 0)

thank you for responding. However these are php pages. The whole site is php. There is no head or footer on any page.

There are references on every page to call variables for header and footer.

<?php include("includes/header.php");?> & <?php include("includes/footer.php");?>
incrediBILL




msg:4542273
 3:56 pm on Feb 4, 2013 (gmt 0)

If there is no header why does your example say include header.php?

Your words, not mine.

If it's all PHP, you can easily put a variable in the canonical tag.

rp024




msg:4542279
 4:11 pm on Feb 4, 2013 (gmt 0)

Sorry, I'm trying to be as specific as possible.

Each page does not have a static header and footer like a standard html page.

They all have references to variables outside of the page. There is no actual <head></head> on any of the sites pages. Only in the actual "include/header.php" file.

We have a canonical on the index. If I add the canonical to the index the way I have it, the page goes blank. If I add it to the header file it works fine except every page gets indexed as index.php that way. Which blows up SEO rank.

Thank you for your help with this.

rp024




msg:4542587
 4:43 pm on Feb 5, 2013 (gmt 0)

Still hoping to find out how to add canonical tag to php pages with no <head></head> tags. Any help is greatly appreciated.

Ex. This is our index.php

<?php
$pageTitle = "";
$description = "";
$keywords = "";
$activePage = "home";


require_once ('includes/header.php');
?>
<div class="clear"></div>
<div class="wrapper" id="bodyWrapper">
<div class="container_12 body">
<div class="grid_8">
<div class="bodyLeft">
<a href="flooring-products-bamboo.php"><img src="images/image.png" width="115" alt="" /></a>
<a href="flooring-products-maple.php"><img src="images/image.png" width="115" alt="" /></a>
<a href="flooring-products-oak.php"><img src="images/image.png" width="115" alt="" /></a>
<a href="flooring-products-exotic.php"><img src="images/image.png" width="115" alt="" /></a>
<a href="flooring-products-laminate.php"><img src="images/laminate.png" width="115" alt="" /></a>
<div class="bodyTitle">
<h1>Welcome to <span class="bold">Title</span></h1>
</div>
<div class="bodyText">
<p>Info</p>

<p>Info</p>

<p>Info</p>

</div>
<div id="homeBodyInside">
<div id="insideLeft">
<div class="insideText clearfix">
<ul style="width:300px" class="left">
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
</ul>
<ul style="width:270px">
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
<li><a href="page.php"></a></li>
</ul>
</div>
</div>
<div class="clear"></div>
</div>
<div class="bodyText">
<p>Info</p>

<p>Info</p>

<p>Info</p>
<a href="page.php">Title</a>
</div>
</div>
</div>

<?php require_once ('includes/sidebar.php'); ?>

<div class="clear"></div>
</div>
</div>

<?php require_once ('includes/footer.php'); ?>

nettulf




msg:4542901
 9:55 am on Feb 6, 2013 (gmt 0)

Please show us the includes/header.php - that is where you would to the changes.

rp024




msg:4543037
 5:55 pm on Feb 6, 2013 (gmt 0)

Thank you for reply @nettulf

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $pageTitle ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/960.css" rel="stylesheet" type="text/css" />
<link href="css/text.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />

<link rel="shortcut icon" href="images/favicon.ico" />
<link href="blog/feed/" type="application/rss+xml" rel="alternate" title="RSS Feed" />

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>
<script type="text/javascript" src="js/jcarousellite_1.0.1.js"></script>
<script type="text/javascript" src="js/superfish.js"></script>
<script type="text/javascript">
$(function(){
$('a[@rel*=lightbox]').lightBox();
});
$(function() {
$(".homeImageBorder").jCarouselLite({
auto: 6000,
speed: 2000,
visible: 1,
scroll: 1,
vertical: true,
btnNext: ".next",
btnPrev: ".prev"
});
});
$(function() {
$(".scrollText").jCarouselLite({
auto: 6000,
speed: 2000,
visible: 1,
scroll: 1,
vertical: true,
btnNext: ".next",
btnPrev: ".prev"
});
});
function search(){
document.searchForm.submit();
};
</script>
<style type="text/css">

.errorField { background:#C00; }

</style>
<!--[if ie 6]>
<style type="text/css">
img, div, input { behavior: url("iepngfix.htc") }
</style>
<![endif]-->
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-13205233-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
<script>

$(document).ready(function(){
$("ul.sf-menu").superfish();
});

</script>
</head>
<body>
<div class="wrapper" id="headerWrapper">
<div class="container_12" id="header1">
<div class="grid_8" id="logo">
</div>
</div>
<div id="phone"><strong class="left">FOR A PHONE QUOTE</strong><h4>xxx xxx xxxx/h4><p>1900 ddddddddddddd | Philadelphia, PA 19146</div>
<div class="searchParts"> <img src="images/search.png" alt="search" /> </div>
<div class="searchParts">
<form action="search-results.php" id="searchForm">
<input type="hidden" name="cx" value="xxxxxxx" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" id="searchQuery" value="Need to Find Something?" onfocus="if(this.value=='Need to Find Something?')this.value='';" onblur="if(this.value=='')this.value='Need to Find Something?';" />
</form>
</div>
<div id="searchButton"><img src="images/searchButton.png" alt="search" onclick="search();" /></div>
<div class="grid_10" id="navigation">
<ul class="sf-menu">www.mysite.com">HOME</a></li>
<li class="<?php if ($activePage == "about"){echo "current";}?>"><a href="about.php">vvvv </a></li>
<li class="<?php if ($activePage == "flooring"){echo "current";}?>"><a href="flooring-products.php">FLOORING PRODUCTS</a>
<ul>
<li><a href="flooring-products-bamboo.php">Bamboo Flooring</a></li>
<li><a href="flooring-products-maple.php">Maple Flooring</a></li>
<li><a href="flooring-products-laminate.php">Laminate Flooring</a></li>
<li><a href="flooring-products-oak.php">Oak Flooring</a></li>
<li><a href="flooring-products-exotic.php">Exotic Flooring</a></li>
</ul>
</li>
<li class="<?php if ($activePage == "stairs"){echo "current";}?>"><a href="stairs-railings.php">STAIRS/RAILINGS</a></li>
<li class="<?php if ($activePage == "installation"){echo "current";}?>"><a href="installation-refinish.php">INSTALL/REFINISH</a></li>
<li class="<?php if ($activePage == "green"){echo "current";}?>"><a href="green-products.php">GREEN PRODUCTS</a></li>
<li class="<?php if ($activePage == "specials"){echo "current";}?>"><a href="specials.php">SPECIALS</a></li>
<li><a href="flooring-products-warranty.php">WARRANTY</a></li>
<li class="<?php if ($activePage == "contact"){echo "current";}?>"><a href="contact.php">CONTACT US</a></li>
</ul>
</div>
</div>

<?php if ($activePage == "home"){ ?>
<div class="container_12" id="header2">
<div class="grid_12">
<div class="homeImageBorder">
<ul>
<li><img src="images/homeImg1.jpg" alt="" width="930" height="243" /></li>
<li><img src="images/homeImg2.jpg" alt="Hardwood Floor " width="930" height="243" /></li>
</ul>
</div>
</div>
<div class="grid_12" id="scrollSection">
<div class="scrollText">
<ul>
<li>
</li>
<li>
</li>

</ul>
</div>
<div class="prevNext"><img src="images/prevNext.png" alt="previous next" border="0" usemap="#Map" /></div>
<map name="Map" id="Map">
<area shape="rect" coords="2,3,27,26" alt="previous" class="prev" href="#" />
<area shape="rect" coords="3,47,28,71" alt="next" class="next" href="#" />
</map>
</div>
<div class="clear"></div>
</div>
<?php } ?>

</div>

incrediBILL




msg:4543073
 7:08 pm on Feb 6, 2013 (gmt 0)

See where the meta description and keywords are at the top of the page?

That's where you would add the canonical as well.

<link rel="canonical" href="<?php echo $canonical; ?>"/>

Your problem is you need something in the variable $canonical.

What exactly are you attempting to accomplish here?

Your site doesn't appear to have additional parameters on the URL which cause problems so were the pages moved and you're trying to tie up a 301 redirect?

Unless you're trying to fix some problem like I mentioned, it's really not needed.

rp024




msg:4543083
 7:14 pm on Feb 6, 2013 (gmt 0)

We do SEO. When we add this canonical tag

<link rel="canonical" href="http://www.mysite.com"/>

Google's indexing sees every single page on the host as "index" since all pages use the same dynamic header.

We want only the index to have the canonical variable to eliminate duplicate indexing. The rest of the pages in we don't want associated with the canonical at all so they get indexed properly.

incrediBILL




msg:4543092
 7:29 pm on Feb 6, 2013 (gmt 0)

OK, I do SEO as well and that's a completely invalid use of the canonical tag and it will bite you in the butt. The canonical tag tells Googlebot which page should be the proper page to be indexed and not all pages should refer back to the main index page.

I think tedster mentioned that you were using it improperly in the second post.

Here's an example of the valid use of the canonical tag if you have URLs like these:

http://example.com/mypage.html?a=1
http://example.com/mypage.html?b=1
http://example.com/mypage.html?a=1&b=1

Google thinks those are 3 different pages instead of one page.

Having all of those pages use the following canonical tag solves the problem of bleeding PR per each parameter.

<link rel="canonical" href="http://example.com/mypage.html"/>

What you're suggesting puts all pages back to the root and while you're right, it might give the home page a minor PR boost, if can cause all the other pages to not be indexed or appear in the SERPs because you've plainly told it that the page they landed on isn't that page.

I have a feeling Google has code that stops people from accidentally shooting themselves in the foot if all pages claim to be the root because they clearly state that the canonical tag is just a suggestion, it can be ignored which it probably is in the way you're implementing it which is 100% wrong.

nettulf




msg:4543095
 7:34 pm on Feb 6, 2013 (gmt 0)

What you need is some code that "computes" the page you are on.

On my shared hosting I have the following code that works on all my pages and subdirectories, even between websites. Maybe you could try?

NB it might give a different result on your host, so make a backup first and look at the output. Maybe show the output here if it does not give the correct result? Put it somewhere between <head> and </head> in the include above.


<?if( basename($_SERVER['PHP_SELF']) == "index" ):?>
<link rel="canonial" href="<?='http://' . $_SERVER['SERVER_NAME']?>">
<?else:?>
<link rel="canonial" href="<?='http://' . $_SERVER['SERVER_NAME'] . parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)?>">
<?endif?>


Edit: Just tested and seems like the if-statement is unnecessary, it shows the homepage correct without it. But the code works so I am leaving it in. :)

[edited by: nettulf at 8:00 pm (utc) on Feb 6, 2013]

rp024




msg:4543098
 7:37 pm on Feb 6, 2013 (gmt 0)

Thanks for this and the last reply, (forgot to mention that in last).

My group actually handles the dev side. We're basically instructed to make specific changes. In this case the SEO/SEM PM's have instructed to add the canonical tag only on the index not on all pages. the page is index.php. And they're adamant the canonical does not reflect index specific because they don't want all pages indexed as such. I was told to use this:

<link rel="canonical" href="http://<?php echo $_SERVER["HTTP_HOST"] ?><?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH); ?>" />

I did this today. Haven't heard back from the PM's yet.

nettulf




msg:4543107
 7:50 pm on Feb 6, 2013 (gmt 0)

That should work. :)

If the include is used in all pages, and you want the canonical for ONLY the homepage, you still could put it in an if-statement like above and skip the <?else:?>. This test if the current filename is "index" - that is the homepage.

rp024




msg:4543113
 8:05 pm on Feb 6, 2013 (gmt 0)

Thank you for the info fellas. Greatly appreciated!

g1smd




msg:4543117
 8:11 pm on Feb 6, 2013 (gmt 0)

The correct URL for the root index page does not include the index page filename. The URL for the root index page should end with a trailing slash after the hostname.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved