Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielmcclure/0d92236a2b4c59dd6da988c193ed187e to your computer and use it in GitHub Desktop.
Save danielmcclure/0d92236a2b4c59dd6da988c193ed187e to your computer and use it in GitHub Desktop.
Export Yoast SEO Redirection rules
<?php
/**
* Generate redirection plugin (https://wordpress.org/plugins/redirection/) compatible csv files from Yoast SEO Premium's redirection.
*
* Usage: download and put this file to root directory of your WordPress installation.
* then visit the url, you will see the csv file :)
* after then WP Adming > Tools > Redirection > import (section)
*
*
* Don't use this file on production
*
*/
require_once 'wp-load.php';
$fileName = 'yoast_to_redirection.csv';
header( 'Content-Type: application/excel' );
header( 'Content-Disposition: attachment; filename="' . $fileName . '"' );
$fp = fopen( 'php://output', 'w' );
$rules = get_option( 'wpseo-premium-redirects-base' );
// header info
fputcsv( $fp, array( "source URL", "target URL", "regex", "http code" ) );
foreach ( $rules as $redirect_item ) {
fputcsv( $fp, array(
'/' . $redirect_item['origin'],
'/' . $redirect_item['url'],
0,
$redirect_item['type'], // http code
) );
}
fclose( $fp );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment