Mass import redirects into Drupal 8

Posted by Bill Seremetis on 2018, March 8 - 16:10

Currently there is no tool to mass import redirects from a CSV file to Drupal 8. The module path_redirect_import tries to solve this, but I had limited success (2%) when using it with D8. So, me and my colleagues created this bash script to solve the problem. The CSV file must be format like this:

source (without leading slash),target (with leading slash)

Here's the script:

cat redir.csv| while read line
let "i++"
rsource=`echo $line | cut -d ',' -f1`
rtarget=`echo $line | cut -d ',' -f2`
redirect='use Drupal\redirect\Entity\Redirect;Redirect::create(["redirect_source" => "'$rsource
if [[ $rtarget = *"http"* ]]; then
redirect+='", "redirect_redirect" => "'$rtarget
redirect+='", "redirect_redirect" => "internal:'$rtarget
redirect+='", "language" => "und", "status_code" => "301",])->save();'
#echo $redirect
drush ev "$redirect"
echo $i $rsource' to ' $rtarget ' added.'