# Simple Redirect List

> Redirects old URLs to new ones with a 301 status from a list you maintain in the snippet. Good for a handful of moved pages.

- Type: PHP
- Runs: Frontend only
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: redirects, 301, urls, seo
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/simple-redirect-list/

## Code

```php
add_action( 'template_redirect', function () {
    // Old path => new path or full URL on this site.
    $redirects = array(
        '/old-page/'    => '/new-page/',
        '/2019/promo/'  => '/offers/',
    );

    $request = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
    $path    = '/' . trim( (string) wp_parse_url( $request, PHP_URL_PATH ), '/' ) . '/';

    foreach ( $redirects as $from => $to ) {
        if ( '/' . trim( $from, '/' ) . '/' !== $path ) {
            continue;
        }

        wp_safe_redirect( preg_match( '#^https?://#', $to ) ? $to : home_url( $to ), 301 );
        exit;
    }
}, 1 );
```

## Notes

Paths are matched without the query string. On a subdirectory install include the subdirectory in the old path. For other domains, add them with the allowed_redirect_hosts filter.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Simple Redirect List" 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/
