Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Url canonical  (Read 1118 times)

atmoner

  • Newbie
  • *
  • Karma: 1
  • Posts: 5
    • atmoner.com
Url canonical
« on: April 16, 2011, 12:30:32 AM »

To avoid duplicate content, it is always good to use the canonical url
Here is a short code simple but very effective:

Open snews.php, search:
Code: [Select]
?>
and add before:
Code: [Select]
$cano = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Open index.php and add this in your header:
Code: [Select]
<link rel="canonical" href="<?php echo $cano?>">
with url rewriting of apache, it's a real pleasure for search engines  ;D
(you can test on my site in sources)
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Url canonical
« Reply #1 on: April 16, 2011, 05:20:43 PM »

Thanks for the good tip Atmoner.

As an alternative...... in snews.php, you can add the $cano variable-value string right at the top of the file, just above the // CONFIGURE DATABASE VARIABLES function, and it will work just fine from there too.
Example:
Code: [Select]
<?php // do not copy this tag

# Canonical URL generator used in <head> of index.php
$cano 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

// CONFIGURE DATABASE VARIABLES

// do not copy this tag 
?>

Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

atmoner

  • Newbie
  • *
  • Karma: 1
  • Posts: 5
    • atmoner.com
Re: Url canonical
« Reply #2 on: April 16, 2011, 09:26:33 PM »

I always add my modifications after the existing ones ... but it will work like this!

Again thank you for your efficient and very simple script  ;)
Logged

mihomes

  • Newbie
  • *
  • Karma: 2
  • Posts: 23
Re: Url canonical
« Reply #3 on: October 31, 2012, 04:45:19 AM »

Made a slight adjustment to this if anyone is interested.  This will remove the c_*/ from any comment pages so the canonical is always the main article page.  The p_1/ is for the first page of articles listings in a directory only... since the index and page 1 are the same.  The other pages will still have their page in the url.

Code: [Select]
$cano = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$cano_array = array('#c_[^/]+/#', '#p_1/#');
$cano = preg_replace($cano_array, '', $cano);
Logged