homepage Welcome to WebmasterWorld Guest from 204.236.255.69
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 Redirection Thru .htaccess - How I Implemented This
Is this safe and ok
3Mice




msg:3820843
 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]

 

phranque




msg:3820917
 11:39 am on Jan 7, 2009 (gmt 0)

that is going to require 3 HTTP requests by the browser after both 301's responses.
it's better to internally rewrite the url and let the page.asp url redirect to the productpage.htm url:
RewriteCond %{QUERY_STRING} ^oldid=(.+)$
RewriteRule ^folder/page.asp$ /redirection.php?id=%1 [L]

jdMorgan




msg:3821028
 2:07 pm on Jan 7, 2009 (gmt 0)

Yes, you should test your current method, and then compare to phranque's method using the Live HTTP Headers add-on for Firefox/Mozilla browsers. You will see that your method give two 'chained' 301 redirects, and search engines are far less likely to pass PageRank/link-popularity through two redirects as through one.

Also, make sure that your script returns a proper 404-Not Found response if the oldid value is invalid!

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved