Custom 404 error page issue I want to have a custom 404 page that displays some additional information. I need to display the requested path that was not found and some pages suggestions based on that path (this is based on some internal logic that I don't think is relevant to my question).

Here is what I did:

I defined the default 404 page to be page-not-found in admin/config/system/site-information

Then in my module I used hook_page_alter() to define my pages suggestions (if drupal_get_http_headers("status") is 404 Not Found), and the request_uri() to be shown on the 404 template page I created.

I have tried to save these values in different ways (session variable, ctools cache, everything I have tried leads to the same issue &mdash; see below).

In mymodule.module:

/**
 * Implement hook_menu
 * @return mixed
 */
function mymodule_menu() {
  $items['page-not-found'] = array(
    'title' =&gt; '',
    'page callback' =&gt; 'mymodule_page_not_found',
    'file' =&gt; 'inc/functions.inc',
    'access callback' =&gt; TRUE,
  );
  return $items;
}

/**
 * Implement hook_theme
 */
function mymodule_theme() {
  return array(
    'mymodule_page_not_found' =&gt; array(
      'template' =&gt; 'templates/404',
    ),
  );
}


In inc/functions.inc:

function mymodule_page_not_found() {

  return theme('mymodule_page_not_found');
}


Problem is, if I request a non existing URL for example mysite.com/not-found-page-1 then request mysite.com/not-found-page-2 the information displayed on the 404 page template when I query the not-found-page-2 corresponds to the values I saved when requesting not-found-page-1.

Any idea why I always get the data of the previous query?
The trouble is, hook_page_alter() is called after your theme function is called.  So if you're setting things there it's too late.

I don't know why you're doing it this way.  Why not generate your suggestions in your page callback: mymodule_page_not_found().  Then pass them to the theme function.

Something like this:

/**
 * Implement hook_theme
 */
function mymodule_theme() {
  return array(
    'mymodule_page_not_found' =&gt; array(
      'template' =&gt; 'templates/404', // Not relevant to your question, but you should consider renaming this to mymodule-page-not-found because of a "bug" in D7; see discussion here: https://www.drupal.org/node/342350
      'variables' =&gt; array('suggestions' =&gt; array()),
    ),
  );
}

function mymodule_page_not_found() {
  // ... generate list of $suggestions ...
  return theme('mymodule_page_not_found', array('suggestions' =&gt; $suggestions));
}