Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Redirecting old urls via php script


mcomet - 11:17 pm on Oct 10, 2012 (gmt 0)


Hello all. I'm attempting to redirect thousands of URLs from an old custom CMS to a new Drupal powered site. The new URLs wont match the old since Drupal auto-assigns page ids. I do have access to the old page ids in my database.

I found [webmasterworld.com ]( Summary: Rewrite all "old" requests to a script. Have the script look up the new URL in a database and force the redirect).

I could use some feedback on my rewrite rules as well as the redirect script.

Old URLs: http://www.example.com/dir1/dir2/view.pl?id=12345
New URLs: http://www.example.com/node/$randomid


// redirect old traffic to redirect script
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^dir1/dir2/view\.pl$ /redir.php? [L]



This is the contents of redir.php:

<?php

// get id from the querystring
$legacy_id = htmlspecialchars($_GET["id"]);

// db Connection
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

//Query
$query = ("SELECT ua.dst
FROM content_type_artwork cta
JOIN url_alias ua
ON ua.src = CONCAT('node/',cta.nid)
WHERE cta.field_legacy_art_id_value = $legacy_id");

$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$new_path = $row['dst'];
}

// close connection
mysql_close($conn);

// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/$new_path");

exit();

?>



Another related question - In the above, I am doing a 301 redirect to /node/$id. Drupal has internal path aliasing for friendly URLs. So, /node/$id will get transformed to something like /path1/page_title. Would it be better for me to redirect to the alias, rather than /node/$id, or does it not matter?

Thanks.
Thanks.


Thread source:: http://www.webmasterworld.com/apache/4506675.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com