Jump to content

Append canonical URL to head

Recommended Posts

Well, In your template file, you could do:

<link href="<?php echo $page->httpUrl; ?>" rel="canonical" />

This would produce something similar to https://domain.com/parentpage/childpage or however your site is set up.

Share this post

Link to post
Share on other sites

Hey Leftfield, it might help us better answer your question if you can describe in more detail your current template set up. For example, I use a template structure where the head is included into every template utilizing 

<?php include("./inc/header.php"); ?>

This way, I call some global settings in the head, and it is included everywhere. However, some people use the delayed output approach. 

Share this post

Link to post
Share on other sites


I got head.php, footer.php, tags.php etc...

I want in tags.php, which doesn't have <head>...</head> in it, to append some Php magic so I could include the line in the head like this one:

<link rel="canonical" href="http://example.com/tags/" />

Something similar to 



Share this post

Link to post
Share on other sites

This is the way I found that works:

1. Create a new folder in your site Modules folder named Injector ( site/modules/Injector ).

2. Copy / paste the following code into a file named Injector.module.php and put it in your module folder.

<?php namespace ProcessWire;
class Injector extends Process {
  public static function getModuleinfo() {
    return [	  
      'title' => 'Injector',	  
      'summary' => 'Add content before </head> and before </body>',	  
      'href' => '',	  
      'author' => '',	  
      'version' => '1.0.0',	   
	  'singular' => true, 
	  'autoload' => true, 
	public function init() {
		// add a hook after each page is rendered and modify the output
		$this->addHookAfter('Page::render', $this, 'after_render');		
    public function after_render($event) {	
		/** @var Page $page */
		$page = $event->object; 

		// ignore templates
		$ignore = array( 'admin' );
		if( !in_array( $page->template, $ignore ) ){
			$replace =array(
				'<!--[+head.include+]-->'  => ( defined( 'HEAD_INCLUDE' ) ? HEAD_INCLUDE : '' ),
				'<!--[+body.include+]-->'  => ( defined( 'BODY_INCLUDE' ) ? BODY_INCLUDE : '' )
			$event->return = str_replace( array_keys( $replace ), array_values( $replace ),$event->return );

4. In your admin, refresh Modules and then install Injector.

5. In your template before the </head> tag, add <!--[+head.include+]-->, also optional add the <!--[+body.include+]--> before the </body> tag.

6. In your script(s) define HEAD_INCLUDE and BODY_INCLUDE ( optional ), ex:

$head_include ='<link rel="canonical" href="http://example.com/tags/" />';

define( 'HEAD_INCLUDE', $head_include );

Do the same thing for BODY_INCLUDE if needed.

Hope this helps.

  • Like 1

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mike Rockett
      Docs & Download: rockettpw/markup-sitemap
      Modules Directory: MarkupSitemap
      Composer: rockett/sitemap
      MarkupSitemap is essentially an upgrade to MarkupSitemapXML by Pete. It adds multi-language support using the built-in LanguageSupportPageNames. Where multi-language pages are available, they are added to the sitemap by means of an alternate link in that page's <url>. Support for listing images in the sitemap on a page-by-page basis and using a sitemap stylesheet are also added.
      Example when using the built-in multi-language profile:
      <url> <loc>http://domain.local/about/</loc> <lastmod>2017-08-27T16:16:32+02:00</lastmod> <xhtml:link rel="alternate" hreflang="en" href="http://domain.local/en/about/"/> <xhtml:link rel="alternate" hreflang="de" href="http://domain.local/de/uber/"/> <xhtml:link rel="alternate" hreflang="fi" href="http://domain.local/fi/tietoja/"/> </url> It also uses a locally maintained fork of a sitemap package by Matthew Davies that assists in automating the process.
      The doesn't use the same sitemap_ignore field available in MarkupSitemapXML. Rather, it renders sitemap options fields in a Page's Settings tab. One of the fields is for excluding a Page from the sitemap, and another is for excluding its children. You can assign which templates get these config fields in the module's configuration (much like you would with MarkupSEO).
      Note that the two exclusion options are mutually exclusive at this point as there may be cases where you don't want to show a parent page, but only its children. Whilst unorthodox, I'm leaving the flexibility there. (The home page cannot be excluded from the sitemap, so the applicable exclusion fields won't be available there.)
      As of December 2017, you can also exclude templates from sitemap access altogether, whilst retaining their settings if previously configured.
      Sitemap also allows you to include images for each page at the template level, and you can disable image output at the page level.
      The module allows you to set the priority on a per-page basis (it's optional and will not be included if not set).
      Lastly, a stylesheet option has also been added. You can use the default one (enabled by default), or set your own.
      Note that if the module is uninstalled, any saved data on a per-page basis is removed. The same thing happens for a specific page when it is deleted after having been trashed.
    • By franciccio-ITALIANO
      Hi, we can choose the "headline" and "title" and "summery" in panel page of processwire, but we can't write the "metadecriptions" and "tags".
       I can write mdescropt and tags in templates, but I've same templates for many articles... so, how I can change mdescription and tags?

    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirectsūüėě
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?
    • By chrizz
      hey there
      I guess a lot of you have already heard of the hreflang attribute which tells search engines which URL they should list on their result pages. For some of my projects I build this manually but now I am wondering if there's need to add this as a module to PW modules directory. 
      How do you deal with the hreflang thingy? Would you you be happy if you can use a module for this or do you have concerns that using a module maybe does not cover your current use cases?
    • By FrancisChung
      Hi, I have an ongoing issue with Google SEO that I can't seem to fix. Wondering if anyone has come across a similar situation?

      We deployed a new version of the website using a new deployment methodology and unfortunately, the wrong robots.txt file was deployed basically telling Googlebot not to scrape the site.

      The end result is that if our target keywords are used for a (Google) search, our website is displayed on the search page with "No information is available for this page." 

      Google provides a link to fix this situation on the search listing, but so far everything I have tried in it hasn't fixed the situation.
      I was wondering if anyone has gone through this scenario and what was the steps to remedy it?
      Or perhaps it has worked and I have misunderstood how it works?

      The steps I have tried in the Google Webmaster Tool :
      Gone through all crawl errors Restored the Robots.txt file and Verified with Robots.txt tester Fetch/Fetch and Render as Google as both Desktop/Mobile, using root URL and other URLs, using Indexing Requested / Indexing Requested for URL and Linked Pages. Uploaded a new Sitemap.xml  Particularly on the Sitemap page, it says 584 submitted, 94 indexed.
      Would the Search Engine return "No Information available" because the page is not indexed? The pages I'm searching for are our 2 most popular keywords and entry points into site. It's also one of 2 most popular category pages.  So I'm thinking it probably isn't the case but ...

      How can I prove / disprove the category pages are being indexed?

      The site in questions is Sprachspielspass.de. The keywords to search are fingerspiele and kindergedichte.

  • Create New...