Create a file called .htaccess in the root of your public_html/htdocs of your domain.

This is useful when you want to handle error pages gracefully instead of showing the default Apache error pages to your visitors. You’ll typically hit this when setting up a new site and want 404s to redirect to your homepage or a custom error page.

Make sure mod_rewrite is enabled on your Apache server, otherwise the RewriteEngine on directive will throw a 500 error. If you’re on shared hosting, most providers have it enabled by default.

Inside add the following text:

Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on

ErrorDocument 404 /index.php
ErrorDocument 301 /index.php

I covered PHP in detail elsewhere.

With the index.php files being the pages that get redirected to.