diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index 11e8b38d25..91a80a897d 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -2205,6 +2205,32 @@ function tracking_unserialize($string, $max_depth = 3)
 }
 
 // Pagination functions
+/**
+* Generate a pagination link based on the url and the page information
+*
+* @param string $base_url is url prepended to all links generated within the function
+*							If you use page numbers inside your controller route, base_url should contains a placeholder (%d)
+*							for the page. Also be sure to specify the pagination path information into the start_name argument
+* @param string $on_page is the page for which we want to generate the link
+* @param string $start_name is the name of the parameter containing the first item of the given page (example: start=20)
+*							If you use page numbers inside your controller route, start name should be the string
+*							that should be removed for the first page (example: /page/%d)
+* @param int $per_page the number of items, posts, etc. to display per page, used to determine the number of pages to produce
+* @return URL for the requested page
+*/
+function phpbb_generate_page_link($base_url, $on_page, $start_name, $per_page)
+{
+
+	if (strpos($start_name, '%d') !== false)
+	{
+		return ($on_page > 1) ? sprintf($base_url, (int) $on_page) : str_replace($start_name, '', $base_url);
+	}
+	else
+	{
+		$url_delim = (strpos($base_url, '?') === false) ? '?' : ((strpos($base_url, '?') === strlen($base_url) - 1) ? '' : '&');
+		return ($on_page > 1) ? $base_url . $url_delim . $start_name . '=' . (($on_page - 1) * $per_page) : $base_url;
+	}
+}
 
 /**
 * Generate template rendered pagination
@@ -2237,8 +2263,6 @@ function phpbb_generate_template_pagination($template, $base_url, $block_var_nam
 	}
 
 	$on_page = floor($start_item / $per_page) + 1;
-	$url_delim = (strpos($base_url, '?') === false) ? '?' : ((strpos($base_url, '?') === strlen($base_url) - 1) ? '' : '&');
-	$page_in_route = strpos($start_name, '%d') !== false;
 
 	if ($reverse_count)
 	{
@@ -2269,14 +2293,7 @@ function phpbb_generate_template_pagination($template, $base_url, $block_var_nam
 	$u_previous_page = $u_next_page = '';
 	if ($on_page != 1)
 	{
-		if ($page_in_route)
-		{
-			$u_previous_page = ($on_page > 2) ? sprintf($base_url, (int) $on_page - 1) : str_replace($start_name, '', $base_url);
-		}
-		else
-		{
-			$u_previous_page = ($on_page > 2) ? $base_url . $url_delim . $start_name . '=' . (($on_page - 2) * $per_page) : $base_url;
-		}
+		$u_previous_page = phpbb_generate_page_link($base_url, $on_page - 1, $start_name, $per_page);
 
 		$template->assign_block_vars($block_var_name, array(
 			'PAGE_NUMBER'	=> '',
@@ -2294,22 +2311,13 @@ function phpbb_generate_template_pagination($template, $base_url, $block_var_nam
 	$at_page = 1;
 	do
 	{
-		if ($page_in_route)
-		{
-			$page_url = ($at_page > 1) ? sprintf($base_url, $at_page) : str_replace($start_name, '', $base_url);
-		}
-		else
-		{
-			$page_url = $base_url . (($at_page == 1) ? '' : $url_delim . $start_name . '=' . (($at_page - 1) * $per_page));
-		}
-
 		// We decide whether to display the ellipsis during the loop. The ellipsis is always
 		// displayed as either the second or penultimate item in the list. So are we at either
 		// of those points and of course do we even need to display it, i.e. is the list starting
 		// on at least page 3 and ending three pages before the final item.
 		$template->assign_block_vars($block_var_name, array(
 			'PAGE_NUMBER'	=> $at_page,
-			'PAGE_URL'		=> $page_url,
+			'PAGE_URL'		=> phpbb_generate_page_link($base_url, $at_page, $start_name, $per_page),
 			'S_IS_CURRENT'	=> (!$ignore_on_page && $at_page == $on_page),
 			'S_IS_NEXT'		=> false,
 			'S_IS_PREV'		=> false,
@@ -2339,14 +2347,7 @@ function phpbb_generate_template_pagination($template, $base_url, $block_var_nam
 
 	if ($on_page != $total_pages)
 	{
-		if ($page_in_route)
-		{
-			$u_next_page = sprintf($base_url, $on_page + 1);
-		}
-		else
-		{
-			$u_next_page = $base_url . $url_delim . $start_name . '=' . ($on_page * $per_page);
-		}
+		$u_next_page = phpbb_generate_page_link($base_url, $on_page + 1, $start_name, $per_page);
 
 		$template->assign_block_vars($block_var_name, array(
 			'PAGE_NUMBER'	=> '',