Last active
May 26, 2024 19:14
-
-
Save alfredo-wpmudev/f884286aeebe8f50dd0bddc7e8b2a783 to your computer and use it in GitHub Desktop.
Convert a CSV file with two columns, where column A is the source URL and column B is the destination
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
<?php | |
/** | |
* Settings your variables | |
* $csv_file handles the name of your CSV file. | |
* $csv_separator is the symbol used to sepparate the columns in the CSV file. | |
* $sc_redirect_file is the final file you will need to import in Smartcrawl Pro -> Tools -> URL Redirection | |
*/ | |
$csv_file = "redirect_list.csv"; | |
$csv_separator = ";"; | |
$sc_redirect_file = "sc_redirect.json"; | |
$row_formatter_data = array( | |
"title"=> "", | |
"source"=> "\/kamloops?utm_camp=gmb", | |
"path"=> "", | |
"destination"=> "\/", | |
"type"=> 301, | |
"options"=> [], | |
"rules"=> [] | |
); | |
$redirect_list = array(); | |
if (($handle = fopen($csv_file , "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1000, $csv_separator)) !== FALSE) { | |
$row_formatter_data["source"]= $data[0]; | |
if (str_contains($row_formatter_data["source"], '.*') || str_contains($row_formatter_data["source"], '(') || str_contains($row_formatter_data["source"], '[')) { | |
$row_formatter_data["path"] = "regex"; | |
$row_formatter_data["options"] = ["regex"]; | |
} | |
else { | |
$path = explode("?", $row_formatter_data["source"]); | |
$row_formatter_data["path"] = $path[0]; | |
$row_formatter_data["options"] = []; | |
} | |
$row_formatter_data["destination"]= $data[1]; | |
$redirect_list[] = $row_formatter_data; | |
} | |
fclose($handle); | |
} | |
$sc_json = json_encode($redirect_list); | |
$fp = fopen($sc_redirect_file, 'w'); | |
if(fwrite($fp, $sc_json)){ | |
echo "Done"; | |
} | |
fclose($fp); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment