Skip to content

Commit

Permalink
Add canonical meta tag to view issue page
Browse files Browse the repository at this point in the history
It is possible for issue URLs to have additional parameters in some 
situations. Search engines such as Google will index better if they 
know which are the set of canonical URLs. It reduces load through 
over-indexing, allows the search engine to prioritise the indexing of 
URLs that are correct, and avoids extraneous or non-ideal URLs getting 
into the index.

Fixes #31833, PR #1862
  • Loading branch information
chrisgraham committed Jan 2, 2023
1 parent 2084a7e commit 488fff8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bug_view_inc.php
Expand Up @@ -110,7 +110,7 @@
compress_enable();

if( $t_show_page_header ) {
layout_page_header( bug_format_summary( $f_issue_id, SUMMARY_CAPTION ), null, 'view-issue-page' );
layout_page_header( bug_format_summary( $f_issue_id, SUMMARY_CAPTION ), null, 'view-issue-page', 'view.php?id=' . $f_issue_id );
layout_page_begin( 'view_all_bug_page.php' );
}

Expand Down
15 changes: 14 additions & 1 deletion core/html_api.php
Expand Up @@ -283,7 +283,7 @@ function html_css_cdn_link( $p_url, $p_hash = '' ) {
* $p_time is the number of seconds to wait before redirecting.
* If we have handled any errors on this page return false and don't redirect.
*
* @param string $p_url The page to redirect: has to be a relative path.
* @param string $p_url The page to redirect: has to be relative to the install path {@see $g_path}.
* @param integer $p_time Seconds to wait for before redirecting.
* @param boolean $p_sanitize Apply string_sanitize_url to passed URL.
* @return boolean
Expand Down Expand Up @@ -311,6 +311,19 @@ function html_meta_redirect( $p_url, $p_time = null, $p_sanitize = true ) {
return true;
}

/**
* Print a canonical meta tag.
*
* @param string $p_url The canonical URL: has to be relative to the install path {@see $g_path}.
*/
function html_meta_canonical( $p_url ) {
$t_url = config_get_global( 'path' ) . $p_url;

$t_url = htmlspecialchars( $t_url );

echo "\t" . '<link rel="canonical" href="' . $t_url . '" />' . "\n";
}

/**
* Require a javascript file to be in html page headers
* @param string $p_script_path Path to javascript file.
Expand Down
12 changes: 8 additions & 4 deletions core/layout_api.php
Expand Up @@ -47,16 +47,20 @@

/**
* Print the page header section
* @param string $p_page_title Html page title.
* @param string $p_redirect_url URL to redirect to if necessary.
* @param string $p_page_id The page id.
* @param string $p_page_title Html page title.
* @param string $p_redirect_url URL to redirect to if necessary: has to be relative to the install path {@see $g_path}.
* @param string $p_page_id The page id.
* @param string $p_canonical_url Canonical URL if necessary: has to be relative to the install path {@see $g_path}.
* @return void
*/
function layout_page_header( $p_page_title = null, $p_redirect_url = null, $p_page_id = null ) {
function layout_page_header( $p_page_title = null, $p_redirect_url = null, $p_page_id = null, $p_canonical_url = null ) {
layout_page_header_begin( $p_page_title );
if( $p_redirect_url !== null ) {
html_meta_redirect( $p_redirect_url );
}
if( $p_canonical_url !== null ) {
html_meta_canonical( $p_canonical_url );
}

layout_page_header_end( $p_page_id );
}
Expand Down

0 comments on commit 488fff8

Please sign in to comment.