php - .htaccess - 404 Error Page with RewriteRule

one text

I am using .htaccess to enforce all www and http requests to non-www https, and adding the trailing / at the end.

Also, I am redirecting all the requests to index.php from where I am handling the routes.

What I had been doing was, checking for routes url and serving the files in index.php. And, also handling the 404 page from there.

However, I came to know it was not good a way to do it.

I tried doing it with ErrorDocument, but I am facing problem, as I am handling all URL to file thing from index.php and, I am having difficulty in combining RewriteRule and ErrorDocument.

Also, am I doing this correctly, or if there is any good approach to do it.

Here is my .htaccess:

<IfModule mod_rewrite.c>

# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# # Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>


<IfModule mod_rewrite.c>
# Turn on rewrite engine
RewriteEngine on

# Adds Trailing slash
#RewriteCond %{REQUEST_URI} !(/$|\.|^$) 
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

ErrorDocument 404 thisshouldleadtoerrorpage

</IfModule>

And this is in the index.php

$url_requested = $_SERVER['REQUEST_URI'];
$url_len = strlen($url_requested);

$actual_path = substr($url_requested, strpos($url_requested,'/'), $url_len); 

    // HOME PAGE
    if($actual_path == "/"){
        include_once( 'pages/home.php');
    }

Could anyone help me with this.

All I want is a custom 404 page satisfying above conditions. Thanks. 

Source