Customize "Access Denied" (403) page for specific content type I'm currently setting up a simple LMS in Drupal 8 and would like to customize the Access Denied page as follows:


Anonymous users should be redirected to the Login page, when they hit a 403 error.
Logged in users trying to access a specific content type (a Course),
which they are not allowed to access, should be redirected to a
custom message (telling them to buy the course or something like
that). It would be even better, if they could be shown the teaser of
that node, but that's not a necessity.
In all other cases, I would like to show a general Access Denied page.


Fwiw, there were similiar questions for D6 here and here that also didn't receive any good replies.

Does anybody happen to know a good solution (for D8)?
Any help would be much appreciated.
You could do that using the rules module.

Create a rule using the Rules module, with as Event something like After visiting node no_access. So that the entire rule configuration would look something like this:

Events: After visiting node no_access

Conditions:
        User has role - Parameter: User: [site:current-user], Roles: anonymous user
        NOT Text comparison - Parameter: Text: [site:current-page:url], Matching text: user/login

Actions: Page redirect - Parameter: URL: user/login

And then for the logged in user, you can create another rule that checks if the user is logged in, and then redirects to a custom page if the user visits a no_access page.

For the general access denied page, you can set a url in Admin &gt; Configuration &gt; System &gt; Site information, under error pages.

Hope it helps