Mass import redirects into Drupal 8

By Bill Seremetis, 8 March, 2018

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:
#!/bin/bash i=0 cat redir.csv| while read line do 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 else redirect+='", "redirect_redirect" => "internal:'$rtarget fi redirect+='", "language" => "und", "status_code" => "301",])->save();' #echo $redirect drush ev "$redirect" echo $i $rsource' to ' $rtarget ' added.' done