Bryan Hadaway Posted May 31, 2010 Share Posted May 31, 2010 I figured out the code that does exactly what I want, now I just need help applying it to the syntax of a different way to serve up PHP. So I need help adapting this code which does just what I want it to: <link rel="canonical" href="http://<?php echo $_SERVER["HTTP_HOST"] ?><?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH); ?>" /> to the syntax of this code: function canlink() { echo '<link rel="canonical" href="http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '" />'; } So how do I apply parse_url and ,PHP_URL_PATH to the syntax of this second code? I've asked this question in so many PHP Forums now, and no one seems to be able to answer this, I'm starting to wonder if it's not possible at all. Thanks, Bryan Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/ Share on other sites More sharing options...
Psycho Posted May 31, 2010 Share Posted May 31, 2010 function canlink() { $request_uri = parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH); echo "<link rel=\"canonical\" href=\"http://{$_SERVER['HTTP_HOST']}{$request_uri}\" />"; } Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1065504 Share on other sites More sharing options...
Bryan Hadaway Posted May 31, 2010 Author Share Posted May 31, 2010 Thanks mjdamato, that worked great. Aaah, and to think how much time I spent on that, so refreshing. Thanks, Bryan Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1065509 Share on other sites More sharing options...
Bryan Hadaway Posted June 7, 2010 Author Share Posted June 7, 2010 Extended solution here: http://www.bryanhadaway.com/super-simple-dynamic-canonical-link-code/ Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1068882 Share on other sites More sharing options...
Psycho Posted June 7, 2010 Share Posted June 7, 2010 You need to review the scripts you are running on that page when it loads. After that page initially loaded it took almost 10 seconds for it to become responsive and it was hitting my CPU and memory usage pretty hard. Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1069043 Share on other sites More sharing options...
mrMarcus Posted June 7, 2010 Share Posted June 7, 2010 That script doesn't work in how canonical references are intended. All it is doing is serving up the current page as the canonical link. Let's say you have two pages with similar/identical information. Your script will generate a canonical link for both. Doesn't really make sense now, does it? The purpose for a canonical link is to tell Google (and other search engines) to use one page over the other when there is a possibility of duplicate content. You are not doing that here. You are simply creating a canonical link for every single page. For example, you have two pages: page one: http://www.example.com/page-one/ and page two: http://www.example.com/page-two/ Now, they both have very, very similar content, but your script will generate a canonical tag for both when the canonical tag on each page should be the same. Both page one and page two should have the following (same) canonical tag (typically): <link rel="canonical" href="http://www.example.com/page-one/" /> That's the proper usage of the canonical tag, otherwise, you might as well just leave it out. Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1069153 Share on other sites More sharing options...
Bryan Hadaway Posted June 8, 2010 Author Share Posted June 8, 2010 @mjdamato Yeah, can't argue with you there, that's the beauty of using WordPress with way too many plugins. I'm actually in the process of validating all the code and seeing what plugins I can cut out and use manual alternatives instead. Just to clarify for anyone reading, that has nothing to do with the canonical link script, this is a side issue we're discussing. @mrMarcus I think you might want to spend a minute or two learning a bit more about the canonical link tag: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139394 as you're at least half right on the purpose of the canonical link, but pretty much wrong on all your points. All it is doing is serving up the current page as the canonical link. Untrue: What will the canonical link tag do for all these variations: http://website.com/ | http://www.website.com/ | http://website.com/index.php | http://www.website.com/index.php | http://website.com/#section1.2 | http://www.website.com/#section1.2 | http://website.com/?=source=twitter+is+cool&id=2654683 | http://www.website.com/?=source=twitter+is+cool&id=2654683 The list goes on an on how just one page can be shown, but it will NOT just mirror the current URL, what it will do for all these variations (and there are a ton more possible variations) is suggest the clean and proper: http://www.website.com/ Search engines are dumb, they can consider some of those pages to be different, that is how it aims to clear up duplicate page issues. Your page 1 and 2 example would only make sense if we were talking about: http://www.example.com/portfolio/?id=page-1 and http://www.example.com/portfolio/?id=page-2 or something like that What you're suggesting would almost certainly be overruled by Google's judgment. It's your job as a webmaster to not create two fully different pages that are or basically are duplicates, remember the canonical link is not a rule and does not necessarily enforce anything, it is only a suggestion. You as a webmaster should never rely on canonical link, you need to be doing everything else right too. You need a good .htaccess file, your own internal link structure should be consistent and show URL's how you want them to be shown. And for the case of your page 2 issue, if you really want to show duplicate or close to duplicate pages and don't want them indexed the proper method would actually be to use the: <meta name="robots" content="noindex" /> tag on any page you wanted to ensure was not indexed. So hopefully, that clears up some of the misconceptions about the canonical link element. Thanks, Bryan Quote Link to comment https://forums.phpfreaks.com/topic/203388-perfect-canonical-link-need-help-altering-syntax/#findComment-1069229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.