Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirection Thru .htaccess - How I Implemented This

Is this safe and ok

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

Junior Member

10+ Year Member

joined:Apr 28, 2003
posts: 43
votes: 0

I had posted a question on this subforum here
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)

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

Here is the code on the redirection.php page

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;
$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"];
return $ret;

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

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

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

he is redirected to


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


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

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


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 10

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]
2:07 pm on Jan 7, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 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!



Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members