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:

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. Oct 30, 2019 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, 2019 at 19:31

2 Answers 2


What I do for pages that I want to mark as canonical is put the following code in my header.php file that is loaded on each page:
<link rel='canonical' href='<?php echo 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>'>

This loads the prefix https:// plus the domain www.example.com plus the page page.php resulting in
<link rel='canonical' href='https://www.example.com/page.php'>

  • this works but have a followup question. Is it possible to add individual canonicals to pages that overwrite the code you have provided above?
    – Rob T
    Nov 25, 2019 at 21:27
  • If you are asking whether it is possible to have this dynamic canonical statement and override it with another statement I would answer yes. For example you could have this one followed by the specific one you want and the last one read will overwrite the previous one. i.e. I could put <link rel='canonical' href='<?php echo 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>'> followed by <link rel='canonical' href='some-other-domain.com/some_other_page.php'> The second one overwrites the first. Hope that helps. Nov 27, 2019 at 5:56
  • I use this method for hreflangs too Jan 3, 2022 at 1:35

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 and acknowledge you have read our privacy policy.

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