Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- 301 Redirection Thru .htaccess - How I Implemented This


3Mice - 8:47 am on Jan 7, 2009 (gmt 0)


I had posted a question on this subforum here
[webmasterworld.com...]
where i wanted to know how to do 301 redirects through the .htaccess file. I got positive responses from all of you..thanks

Now the reason i asked was that the current site i am managing has been completely redesigned from scratch. Initially it was hosted on a Windows server but now will be on a Linux server. The problem i faced was that the old site has hundreds of links in this form http://www.example.com/folder/page.asp?oldid=1 where the id changes and these links were bringing tremendous traffic indexed by google..

Obviously when we moved to the Linux server the .asp pages would not work so the problem was to figure how to redirect the search engine traffic coming in from the .asp pages to redirect to the new pages on the linux server..

The new page link look something like this http://www.example.com/folder/productpage.htm

The first thing i did on the .htaccess file was to add this code..

RewriteCond %{QUERY_STRING} ^oldid=(.+)$
RewriteRule ^folder/page.asp$ http://www.example.com/redirection.php?id=%1 [R=301,L]

to redirect any traffic coming in from the old .asp pages to the redirection.php page to get the new link corresponding to the old id.

I created a table called redirection in the database that stores the old id number in one column (id_number) and the corresponding link of the new page in the next column (new_link)
eg

Column name ¦ Value
=========== =====
id_number ---> 1
new_link ---> folder/productpage.htm

Here is the code on the redirection.php page

===
<?php
function getRedirectUrl($productid) {
// Connect to the database
$dServer = "localhost";
$dDb = "database";
$dUser = "username";
$dPass = "password";

$s = @mysql_connect($dServer, $dUser, $dPass)
or die("Couldn't connect to database server");

@mysql_select_db($dDb, $s)
or die("Couldn't connect to database");

$query = "SELECT new_link FROM redirection WHERE id_number = ". $productid;
mysql_query($query);
$result = mysql_query($query);
$hasRecords = mysql_num_rows($result) == 0 ? false : true;
if (!$hasRecords) {
$ret = 'http://www.mysite.com/';
} else {
while($row = mysql_fetch_array($result))
{
$ret = 'http://www.example.com/'. $row["new_link"];
}
}
mysql_close($s);
return $ret;
}

$productid = $_GET["id"];
$url = getRedirectUrl($productid);

header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit();
?>

========
So if someone comes to our site through the old link http://www.example.com/folder/page.asp?oldid=1

he is redirected to

http://www.example.com/folder/productpage.htm

with the 301 headers sent to the search engine..What do you think? Is this good and safe enough?

So far on the local machine everything is working ok

Thanks

[edited by: jdMorgan at 2:09 pm (utc) on Jan. 7, 2009]
[edit reason] Please use example.com -- It can never be owned. [/edit]


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