0

First, I want to preface that I am not very good with php so I am looking for some guidance. I'm working on a website using Dreamweaver that is being accessed through .davaccess

each php page has the following variables:

<?php
require_once "../config.php";
$title="Custom Title I enter";
$metaDescription="Custom Description I enter.";        
include_once (INCLUDES_PATH  . "/header.php");
?>  

And the header.php includes this section:

<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $metaDescription ?>" />
<base href="<?php echo $config['urls']['baseUrl'] ?>">

My thought is that I need to add something in the header file so I can add my own URL on the individual pages. Based on how the title and description have worked, I tried including the following:

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

but that ends up just making each page canonicalize to the base URL, before I add anything to individual pages.

What I'm looking for is a way to add some code that can autopopulate the current URL as the canonical, and possibly update it on individual pages if necessary. Any help would be greatly appreciated. Thank you

  • 2
    Setting the current URL to the canonical is the same as not having a canonical tag. Your canonical tag should compute what the ideal URL should be so that even if a search engine crawler gets to the page with an alternate URL, the canonical tells search engines about the preferred one. – Stephen Ostermiller Oct 30 at 17:09
  • I wouldn't say a self-referencing canonical is the same as not having one. If there is no canonical search engines can do what they will with it. The real goal here is to add a canonical to each page that points to the desired version. The current as canonical was just a quick starting point to where I can make individual edits. – Rob T Oct 30 at 19:31
0

You can set your canonical to be the current page URL. According to Get the full URL in PHP on stackoverflow.com, you just need to initiate your $canonical variable like:

$canonical = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Maybe right where your $title is set would also be a good place to do that.


However, the better way to set the canonical is by computing it. You don't say how your URLs are structured, so I will give you an example from this site. Lets say your URL is /questions/125939/need-help-setting-canonical-within-php.

Your rewrite rules might modify that to /show-question.php?question-id=125939.

Pseudocode for computing the URL might look like:

questionId = getCgiVar('question-id')
questionTitle = getQuestionTitleFromDatabase(questionId)
urlSlug = questionTitle.lowercase().replace(" ", "-")
canonical = "https://example.com/questions/" + questionId + "/" + urlSlug

Hard coding https and the domain name in the canonical ensure that the canonical doesn't include alternate versions that are unlike your preferred URLs. Looking up the URL slug from the database ensures that the canonical URL has the correct slug even if the question title changes or if somebody links to a misspelling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.