Here’s a simple Perl script that can be used to convert a CSV file of redirects into Apache rewrite rules:
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/perl while(<STDIN>) { chomp(); ($f_url,$t_url) = split(/\,/); if ($f_url =~ /^(.*?)\?(.*)/) { print "RewriteCond %{QUERY_STRING} ^" . $2 . "\$\n"; $f_url = $1; }; $f_url =~ s/^\/(.*)$/$1/gm; print "RewriteRule ^" . $f_url . " " . $t_url . "? [R=permanent,L]\n"; print "\n"; } |
The script assumes that you have 2 column – the source URL and the destination URL. You might invoke the script like this:
1 |
cat myredirects.csv | ./makerules.pl >> .htaccess |
Sometimes the CSV file will be DOS formatted to I’ll do:
1 |
cat myredirects.csv | dos2unix | ./makerules.pl >> .htaccess |
There are may ways that this script could be expanded or improved but it’s a good starting point for the typical case.