How to remove index.php from URL and set view variables in 404 template

Hi, in my application I have the if i visit my page of http://nanocord.io/ or http://nanocord.io/index.php/ they are the same exact thing. I would like what is the best way in the case that someone visits like this: http://nanocord.io/index.php/ to redirect http://nanocord.io/. So prettu much hiding the index.php.

Have tried the following in .htaccess:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

RewriteRule ^(.+)$ index.php/$1 [L]

Also when I type any other value where the routes don’t match like so: http://nanocord.io/testing it gives out the error of 404 which is correct but I am sending variables to the layout when the route gets matched so how can I send them also for response error codes?