Skip to content

Instantly share code, notes, and snippets.

@andrewrjones
Created April 26, 2013 22:37
Show Gist options
  • Select an option

  • Save andrewrjones/5470909 to your computer and use it in GitHub Desktop.

Select an option

Save andrewrjones/5470909 to your computer and use it in GitHub Desktop.
Script to migrate short URLs from YOURLs to nginx.
#!/usr/bin/php
<?php
/*
This script takes all the short URL's in the YOURLs database and generates
an nginx config file containing the correct redirects.
Use if you want to migrate away from YOURLs and use nginx to ensure the links
continue to work.
Set the variables in capitals, then run the script. It will print the config
to STDOUT.
*/
# the nginx server
$SERVER = "example.com";
# DB
$DB_HOST = "localhost";
$DB_USER = "yourls";
$DB_PASS = "password";
$DB_DATABASE = "yourls";
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_DATABASE);
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit(1);
}
$result = mysqli_query($con, "SELECT keyword, url FROM yourls_url");
printf("server {\n");
printf(" server_name %s;\n\n", $SERVER);
while($row = mysqli_fetch_array($result)) {
printf(" location = \"/%s\" {\n", $row['keyword']);
printf(" return 301 %s;\n", $row['url']);
printf(" }\n");
}
printf("}\n");
mysqli_close($con);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment