Manually insert canonical url in OJS header.tpl

I want to insert a canonical link for my OJS website. I managed to find the header.tpl and it looks like this:

{**
 * templates/common/header.tpl
 *
 * Copyright (c) 2013-2015 Simon Fraser University Library
 * Copyright (c) 2003-2015 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * Common site header.
 *}
{strip}
{translate|assign:"applicationName" key="common.openJournalSystems"}
{include file="core:common/header.tpl"}
{/strip}

I wand to add some block of code to specify the canonical url for my site so i did the following:

{**
 * templates/common/header.tpl
 *
 * Copyright (c) 2013-2015 Simon Fraser University Library
 * Copyright (c) 2003-2015 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * Common site header.
 *}
{strip}
{translate|assign:"applicationName" key="common.openJournalSystems"}
{include file="core:common/header.tpl"}
{/strip}
<?php
if ( is_front_page() )
    echo '<link rel="canonical" href="' . home_url( '/' ) . '" />';
?>

Will the above work? or cause my site to break. Is there a better way to achieve what i want to do? I’m suspecting plugins but could not identify any to get the job done. My website is running on OJS 2.4.8. Plan to upgrade to 3.0 soon

Thanks for all the help.

@asmecher. Hi Alec, any help on this? thanks.

Is there a reason you only want to output a canonical url for the homepage? Generally the canonical url would be used for any set of pages where the content is available via multiple possible paths.

If your site is available both via www.someurl.tld and via someurl.tld without the www, only one of the two should be in the canonical URL for each page. There is a bit of code in the UsageEvent plugin which does this logic:

I’m becoming more convinced that this kind of function needs to be propagated to the core (probably in PKPRequest.inc.php ?), but it hasn’t been yet.

I could imagine a function:

	/**
	 * Generate a canonical URL to the specified page within a PKP Application.
	 * @param $context Array The optional contextual paths
	 * @param $page string The name of the op to redirect to.
	 * @param $op string optional The name of the op to redirect to.
	 * @param $path mixed string or array containing path info for redirect.
	 * @param $params array Map of name => value pairs for additional parameters
	 * @param $anchor string Name of desired anchor on the target page
	 * @return string The canonical URL
	 */
	function getCanonicalUrl($context = null, $page = null, $op = null, $path = null, $params = null, $anchor = null) {

This would be assigned to the template in the PKPTemplateManger:

And output in the head of each page via the common/header.tpl:

There would probably need to be an option to enable / disable this per Site or per Journal, maybe?

1 Like

@ctgraham:

I’m becoming more convinced that this kind of function needs to be propagated to the core (probably in PKPRequest.inc.php ?), but it hasn’t been yet.

Hi Clinton, you sure ain’t making no mistake here as it helps every ojs user. I actually want to insert the canonical link on every page header to assist with SEO. Is there any thing i could do to achieve this in the meantime?

Dear @ctgraham, @asmecher thank you for your job.
My OJS version is OJS 2.4.3.0.
I’m really interested to this topic and in particular to the solution that you have posted:

about to exploit the code in the UsageEvent plugin.
It seems that Googlebots need to identify a canonical url in the pages of our web site in order to index correctly the content of the pages.
Many pages of the website are excluded by the coverage in Google Search Indexing for one of the following reasons: “the Duplicate without user-selected canonical” or “Duplicate, submitted URL not selected as canonical”.
Could you clarify if your idea cited in the post is achievable or not?
Are there news about this issue?
Thanks for the help.
Francesco

This has not be implemented at this time. If you have a base_url[] set per journal, OJS formulates all URLs from that setting, and this change would be fairly easy to effect. If you don’t have a base_url per journal, OJS operates with relative links from whatever URL the webserver is acting as. This configuration is probably common, and it is where identifying a canonical URL would be important. If a change like this was made under the current framework, we would need to require everyone to consistently describe their canonical URL in the config.inc.php. This make the overall change a bit more complex, but I still do think this is important.