homepage Welcome to WebmasterWorld Guest from 54.227.25.58
register, free tools, login, search, subscribe, 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

    
Canonical link with PHP
AlexB77




msg:4404757
 2:50 pm on Jan 7, 2012 (gmt 0)

Hi Guys,

I am trying to use the following on the template to automatically generate canonical links on every page when page is requested.

<link rel="canonical" href="<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];echo $url;?>">


this works just fine, but only if your page is static .html doc with no query string in it such as ".html?blablabla"

Question:

Is there any way to clean the output so that it only outputs the actual link to be only .html and without any query string attached, even if there is one in the request URL?

 

penders




msg:4404777
 4:12 pm on Jan 7, 2012 (gmt 0)

Try SCRIPT_NAME instead of REQUEST_URI.

AlexB77




msg:4404792
 4:45 pm on Jan 7, 2012 (gmt 0)

Worked like it should have.

Thanks!

Is there any security risks with using following on static .html pages?

<link rel="canonical" <?php
function fetch_url() {
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
return $url;}
echo "href=\"" . fetch_url() . "\">";?>

penders




msg:4404798
 5:42 pm on Jan 7, 2012 (gmt 0)

Presumably you are parsing .html pages with PHP?

I think this is OK regarding security.

Just a thought.... however, you could tidy it up and define the function at the start of your script. It could be more useful if fetch_url() returned just the URL? Or have a function that returns the whole canonical tag? But if you are not using the function anywhere else then you don't need a function for this.

AlexB77




msg:4404828
 8:18 pm on Jan 7, 2012 (gmt 0)

I thought so too and decided against it. I think that instead I will just use it like below:

<link rel="canonical" href="<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];echo $url;?>">

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