Infinite Scroll and pagination problem

Hello!

I’ve got a collection of pages with different templates stored in multiple subfolders as categories. This collection is then ordered by date and time. I reached the following solution to achieve this:

<?php
$cards = page('articles')->index()->visible()->filterBy('template', 'static.cards');
$video = page('articles')->index()->visible()->filterBy('template', 'static.video');
$post_a = page('articles')->index()->visible()->filterBy('template', 'post.a');
$post_b = page('articles')->index()->visible()->filterBy('template', 'post.b');
$totalContent = new Pages(array($cards, $video, $post_a, $post_b));
$totalContent = $totalContent->sortBy('fecha', 'asc', 'time', 'asc')->flip()->paginate(9);
$pagination = $totalContent->pagination();
?>

That worked as expected. I now have a collection with pagination.

I’ve managed to use the <?php if(kirby()->request()->ajax()): ?> function to structure all of my templates and make them work with Infinite Scroll and History.js in order to load a lot of content through AJAX.

Whenever the site is loaded from the root url, the pagination works as expected. Here’s the code for that part:

<?php if($pagination->hasPages()): ?>
  <nav class="pagination end">
	<?php if($pagination->hasNextPage()): ?>
	<a class="next" href="<?php echo $pagination->nextPageURL() ?>"></a>
	<?php else: ?>
<p> Congrats, you’ve reached the end of the internet. </p>
  <?php endif ?>
  </nav>
<?php endif ?>

The pagination link outputs as:

<a class="next" href="/root-url/page;2"></a>

The problem happens when I enter from a different link, let’s say, an article or post from the collection. A modal window is used to load the article while the post-loading feed stays the same way. In this case pagination outputs as:

<a class="next" href="/root-url/articles/post-title-example/page;2"></a>

This breaks the whole loading.

My question is: How can I make a pagination always to point to the root url? Does the way I created the collection have something to do with this error?