There are 2 options to do so, without any custom code involved, as detailed below.
Option 1: Use the CustomError module
The CustomError module allows the site admin to create custom error pages for HTTP status codes 403 (access denied) and 404 (not found), without creating nodes for each of them. Some more details about its features (from its project page):
- Configurable page title and descriptions.
- There are no author and date/time headers as with normal nodes.
- Any HTML formatted text can be be put in the page body.
- The error pages are themable.
- Users who are not logged in and try to access an area that requires login will be redirected to the page they were trying to access after they login.
- Allows custom redirects for 404s.
You'll probably be interested mostly in the part about "Users who are not logged in and try to access an area that requires login will be redirected to the page they were trying to access after they login.".
For D8 there is a 8.x-1.x-dev version for this module available also, more details about it can be found in Issue # 2219227.
Option 2: Use the Rules module
Assume the path of the "Default 403" page is set to
no_access (via admin). Then create a rule using the Rules module, with as Event something like "After visiting node
no_access". So that the entire rule would look something like so:
Should you want to do so, you could even add another Action to also display some (informational) message in the Drupal message area, with something like "You tried to visit a page for which login is required ...".
The above rule is for role anonymous. Create a similar rule for each other role you want to handle in the same way. Using the Conditional rules module you could even merge them in a single rule.
True, it might require you to enable an extra contributed module (Rules). But, as indicated by its growing popularity also, that module is probably already enabled in mostly any site (similar to the Views module), because there are dozens of use-cases for this module.
For D8 there is a 8.x-3.x-alfa version for this module available also.