# Redirect 404 Pages to the Homepage

> Sends visitors who hit a missing page to the homepage instead of showing the 404 template.

- Type: PHP
- Runs: Run everywhere
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: redirects, 404, not-found
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-404-to-homepage/

## Code

```php
add_action( 'template_redirect', function () {
    if ( is_404() ) {
        wp_safe_redirect( home_url( '/' ), 301 );
        exit;
    }
}, 99 );
```

## Notes

Search engines treat mass redirects to the homepage as soft 404s. A helpful 404 page or specific redirects are usually better.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect 404 Pages to the Homepage" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
