Halloween Sale 15% off Ends in See the plans
PHP

Keep Old Feed Addresses Working After Moving to WordPress

Redirects feed addresses used by Blogger, Ghost, Jekyll, Hugo and other platforms, such as /rss.xml or /feeds/posts/default, to your WordPress feed, so existing subscribers keep receiving posts.

Don’t have WPIDE yet? Install it on your site

Code

// Old feed addresses (from Blogger, Ghost, Jekyll, Squarespace ...) that should lead to your WordPress feed.
$wpide_old_feed_paths = array( '/rss.xml', '/feed.xml', '/atom.xml', '/index.xml', '/index.rdf', '/rss', '/feeds/posts/default' );

add_action( 'template_redirect', function () use ( $wpide_old_feed_paths ) {
    if ( ! is_404() ) {
        return;
    }

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

    if ( '' === $path ) {
        return;
    }

    // Path relative to the site, so subdirectory installs work too.
    $relative = '/' . ltrim( substr( $path, strlen( untrailingslashit( $base ) ) ), '/' );
    $relative = strtolower( untrailingslashit( $relative ) );

    foreach ( $wpide_old_feed_paths as $old ) {
        if ( strtolower( untrailingslashit( $old ) ) === $relative ) {
            wp_safe_redirect( get_feed_link(), 301 );
            exit;
        }
    }
} );

Notes

Only addresses that would otherwise be a 404 are redirected; add any other old feed paths to the list.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.