Last active
August 29, 2015 13:56
-
-
Save justinribeiro/9276714 to your computer and use it in GitHub Desktop.
Creates a column full of 301 redirects from published Wordpress post based on permalink.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Wordpress perma to 301 redirect | |
-- | |
-- This script will output a column called rewrite_rule with a set of 301 redirect rules for an .htaccess file. | |
-- | |
SET @table_prefix = "wp_"; | |
SET @url_base_target = "http://www.soemthing.new/blog"; | |
SET @generateSql = CONCAT(" | |
SELECT | |
wpp.guid, | |
wpp.post_date, | |
CONCAT(wpo_su.option_value, | |
REPLACE( | |
REPLACE( | |
REPLACE( | |
REPLACE(wpo.option_value, | |
'%year%', DATE_FORMAT(wpp.post_date, '%Y')), | |
'%monthnum%', DATE_FORMAT(wpp.post_date, '%m')), | |
'%day%', DATE_FORMAT(wpp.post_date, '%d')), | |
'%postname%', wpp.post_name) | |
) AS permalink, | |
CONCAT('", @url_base_target, "', | |
REPLACE( | |
REPLACE( | |
REPLACE( | |
REPLACE(wpo.option_value, | |
'%year%', DATE_FORMAT(wpp.post_date, '%Y')), | |
'%monthnum%', DATE_FORMAT(wpp.post_date, '%m')), | |
'%day%', DATE_FORMAT(wpp.post_date, '%d')), | |
'%postname%', wpp.post_name) | |
) AS target, | |
CONCAT( | |
'RewriteCond %{HTTP_HOST} ^', | |
REPLACE( | |
REPLACE( | |
REPLACE(wpo_su.option_value, | |
'https://', ''), | |
'http://', ''), | |
'.', '\.'), | |
'$ [NC]\n', | |
'RewriteCond %{QUERY_STRING} ^$\n', | |
'RewriteRule ^', | |
REPLACE( | |
REPLACE( | |
REPLACE( | |
REPLACE(wpo.option_value, | |
'%year%', DATE_FORMAT(wpp.post_date, '%Y')), | |
'%monthnum%', DATE_FORMAT(wpp.post_date, '%m')), | |
'%day%', DATE_FORMAT(wpp.post_date, '%d')), | |
'%postname%', wpp.post_name), | |
'$ ', | |
'", @url_base_target, "', | |
REPLACE( | |
REPLACE( | |
REPLACE( | |
REPLACE(wpo.option_value, | |
'%year%', DATE_FORMAT(wpp.post_date, '%Y')), | |
'%monthnum%', DATE_FORMAT(wpp.post_date, '%m')), | |
'%day%', DATE_FORMAT(wpp.post_date, '%d')), | |
'%postname%', wpp.post_name), | |
'? [R=301,NE,NC,L]\n' | |
) AS rewrite_rule | |
FROM ", @table_prefix, "_posts wpp | |
INNER JOIN ", @table_prefix, "_options wpo | |
ON wpo.option_name = 'permalink_structure' | |
INNER JOIN ", @table_prefix, "_options wpo_su | |
ON wpo_su.option_name = 'siteurl' | |
WHERE wpp.post_type = 'post' AND wpp.post_status = 'publish' | |
ORDER BY wpp.post_date DESC" | |
); | |
PREPARE outputStatement FROM @generateSql; | |
EXECUTE outputStatement; | |
DEALLOCATE PREPARE outputStatement; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of the output in the rewrite_rule column: