Cookie Consent by Free Privacy Policy Generator

PHP Superglobals for creating Canonical Tag & OG Tag

Save time on manually populating canonical tags and open-graph (OG) using a simple PHP snippet. This method will fetch the current URL loaded in the browser.

Replace your canonical and OG tag with the following:
Code:
<!-- canonical tag -->
<link rel="canonical" href="<?php $url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];echo $url;?>">

<!-- OG URL -->
<meta property="og:url" href="<?php $url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];echo $url;?>">

The output will look similar to this:

og-canonical.JPG


This method is SEO friendly, however, if you are canonicalising URLs (non-self-referencing). You will need to override the canonical to point at the new URL. This 'PHP echo URL for Canonical Tag & OG Tag' is meant for basic CMSes and semi-dynamic websites created in PHP. Popular CMSes such as WordPress and Drupal will dynamically create a canonical and possibly the OG URL.
 
Back
Top