Problem/Motivation

If we're unable to find an alternative page to redirect to when handling a 404, we may want to show a custom 404 page instead. However in this case we should return a 404 status code so search engines know the page they're trying to reach is now gone with no replacement. However, the current implementation issues a redirect to the custom 404 page itself, so we end up with 30x + 200 status codes, and search engines are effectively being told that the custom 404 page content replaces whatever was previously at the not found URL, which is Not Good.

Proposed resolution

The existing custom controller approach should be refactored as an event subscriber in similar fashion to the core CustomPageExceptionHtmlSubscriber, with a higher priority so that it runs in preference to the core implementation. This is quite a big change, potentially worthy of a 2.x change as it will likely impact on configuration.

Comments

pstewart created an issue. See original summary.

zeip’s picture

Status: Active » Needs review
StatusFileSize
new3.61 KB

It would probably make sense to implement it as suggested in the original issue. However I took a simpler approach at fixing this by just using a subrequest and it seems to work quite well at least for us; could use some tweaking regarding the language handling.

pstewart’s picture

Status: Needs review » Needs work

Thanks for the patch - that's an interesting approach, I was not aware of subrequests! I'm not in a position to try the patch right now, but it looks pretty much OK. I think the only thing I'd want to change is as you say with the language handling. I don't know much about multilingual Drupal: it looks like a custom language negotiator to enforce the current language on the subrequest would be preferrable, but the path hack you've got is probably good enough for a quick fix: I'd just want to tweak it to add a check to see if the site is multilingual before modifying the request path (e.g. $this->languageManager->isMultilingual()), so that sites that don't have the language module enabled can just happily pass the unchanged path on to the subrequest. Also, small quibble: it looks like you missed changing Drupal::languageManager() call over to the injected $this->languageManager :-)

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new3.86 KB

True! I added a multisite detection and fixed the LanguageManager to use the injected one.

zeip’s picture

StatusFileSize
new3.86 KB
new2.04 KB

... and just one more patch to fix the camelcase in the response variable.

pstewart’s picture

Thanks for this ZeiP - I've just tried out #5 on my dev site and can confirm it is working well. I'll commit this in and tag a new beta release. I've opened #3269069: Replace custom controller with an http exception event subscriber for the http exception event subscriber refactor idea, which can now be a longer term piece of work rather than a requirement for fixing this issue, as your subrequest approach is a good solution to the redirect-instead-of-404 problem being addressed in this issue.

  • pstewart committed 7c1031b on 1.x authored by ZeiP
    Issue #3219321 by ZeiP, pstewart: Redirecting to custom 404 page is an...
pstewart’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.