Jump to content
Leftfield

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

OK.

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 

headScripts->append

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By neophron
      Hi guys,
      after getting a complain message from google about a robots.txt (where everything is ok), I searched for an online tool, where I can test my robots.txt files. I found this website: https://technicalseo.com/tools/
      This page offers a bunch of nice tools, just wanted to share it with you.

    • By Mike Rockett
      Docs & Download: rockettpw/seo/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 NehaPillai
      Hello Everyone, I was trying to update SEO meta title, description and meta keywords for my website in Process Wire CMS but it saving in the backend but it is not reflecting on my website, Please help me regarding this error. Please find below attached screen shot for your ref. TIA.


    • 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?

      Thanks...
    • 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?
×
×
  • Create New...