Skip to content

Instantly share code, notes, and snippets.

@mustafauysal
Created November 5, 2017 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mustafauysal/bdd383f998dbf81a0aa890625f3a4a46 to your computer and use it in GitHub Desktop.
Save mustafauysal/bdd383f998dbf81a0aa890625f3a4a46 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 );
@danielmcclure
Copy link

Thank you, such a useful tool since the export is hidden if you downgrade a site!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment