0

I'm trying to set a custom 404 page but is not working. What I tried:

  • configuration - system - basic site settings and set node path on 404 page -> didn't work
  • add page--404.html.twig to my custom theme -> didn't work
  • in my custom-theme.theme added _theme_suggestions_page_alter function -> didn't work

Always show default 404 page, not my custom page. I clear cache on every attempt. Is there something I am missing?

Thanks in advance


Edited

Tried on local and cloud acquia server

I check with another websites that I build and all of them give me this 404 page (default) default 404 page

But with this specific website I get this 404 page 404 page

It seems this website 404 error is handle by browser and not by drupal 8(?)


Edited 2

Another thing I notice is when I type a wrong URL appear on "log messages"

log message

But on current website (the one I can't set custom 404) I don't have any error on "log messages".

Is there any code/config to enable/disable this?

Maybe this is same problem?

8
  • Is it on Local or Remote server?
    – Beebee
    Sep 29 '20 at 10:58
  • Tried on both, local and cloud acquia server
    – Pedro
    Sep 29 '20 at 11:00
  • That screenshot looks like devportal.dd can't be mapped to an IP address (hostname unresolved) so the browser gives you 404. This has nothing to do with Drupal being able to find a page. Looks like your server isn't configured properly.
    – Beebee
    Sep 29 '20 at 11:41
  • 1
    This is an empty 404 response with no text, then the browser displays a standard text. Obviously something on your site is sending such a response before Drupal has a chance to log the error and send a themed error page (whether this contains the Drupal standard text or a custom one doesn't matter).
    – 4k4
    Sep 29 '20 at 12:24
  • 1
    This could be an attempt to serve fast 404's. Drupal 8 provides some basic fast 404 features configurable in settings.php, which are normally only used for certain file types. Then it could be a contrib module or custom code.
    – 4k4
    Sep 29 '20 at 12:42
1

Are your sure you did all the steps? Step 1: in mytheme.theme

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_page_alter(array &$suggestions, array $variables) {

  $route_name = \Drupal::routeMatch()->getRouteName();
  switch ($route_name) {
    case 'system.401':
      // Unauthorized Access.
      $error = 401;
      break;

    case 'system.403':
      // Access Denied.
      $error = 403;
      break;

    case 'system.404':
      // Page Not Found.
      $error = 404;
      break;
  }
  if (isset($error)) {
    $suggestions[] = 'page__' . $error;
  }
}

Step 2: create the page--404.html.twig file

Step 3: clear cache

For some markup, check https://stefvanlooveren.me/blog/styling-404-error-pages-drupal-8

4
  • Thanks for your help. Yes I treid that function with my custom twig and didn't work :/ I found your link on my search for this error :)
    – Pedro
    Sep 29 '20 at 11:23
  • Are you sure you used MYTHEME_theme_suggestions_page_alter and not MYTHEME_suggestions_page_alter? update: After your edit I think this is a server-side issue. Sep 29 '20 at 11:25
  • Yes, I set MYTHEME_theme_suggestions and just to be sure also tried MYTHEME_suggestions. This could be some config from acquia cloud? In htaccess don't have any difference between this site and other's where custom 404 works
    – Pedro
    Sep 29 '20 at 11:35
  • This can't be the issue here, the theme suggestion is in core since 8.8.0. See drupal.org/node/2960810
    – 4k4
    Sep 29 '20 at 11:57
1

Thanks everyone who contrib to help me solve this problem.

Special thanks to @4k4 user, with some keywords and starting points I could found where my problem was.

It was in settings.php at "Fast 404 pages" function, I had this code:

settings.php

Commented this block and it works like charm.

Thanks @4k4 for your help.

0
/**
 * Implements hook_theme_suggestions_page_alter().
 */
function THEMENAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
        $path_args = explode('/', trim(\Drupal::service('path.current')->getPath(), '/'));
        $suggestions = theme_get_suggestions($path_args, 'page');
        $http_error_suggestions = [
            'system.401' => 'page__401',
            'system.403' => 'page__403',
            'system.404' => 'page__404',
        ];

        $route_name = \Drupal::routeMatch()->getRouteName();
        if (isset($http_error_suggestions[$route_name])) {
            $suggestions[] = $http_error_suggestions[$route_name];
        }
}

Create the page template page--404.html.twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.