Jump to content

Help With PHP Redirect....Easy I Think??

- - - - -

  • Please log in to reply
5 replies to this topic

#1
kylel

kylel

    Newbie

  • Members
  • Pip
  • 2 posts
Okay, I thought this was going to be easy but I have been asking around on a different forum and no one seems to know. I decided to come here because....quite frankly....people here seem to be quite smart and actually know what they are doing...so here goes...

I am currently using the script below to redirect search engines(not users):

<?php

if (preg_match("#(google|slurp@inktomi|yahoo! slurp|msnbot)#si", $_SERVER['HTTP_USER_AGENT'])) {

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: http://new-location.com");

	exit;

}

?>

All I want to do is reverse the script so that users are redirected and not bots....I know I could just use the "if not equal to" in the script above....but I want ALL bots to not be redirected....not just the search engines. Basically something like the script below.

<?php

if (preg_match("#(ANY USER THAT IS NOT A BOT)#si", $_SERVER['HTTP_USER_AGENT'])) {


[COLOR="orange"]//Obviously replace the "ANY USER THAT IS NOT A BOT" with a script that actually works.[/COLOR]


    header("HTTP/1.1 301 Moved Permanently");

    header("Location: http://new-location.com");

	exit;

}

?>

So yeah, if you can edit that script in a way that it would actually work, that would be fantastic!:w00t: Thanks a bunch!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
User agents are unique, you likely will have better luck doing the not equal to on the first mentioned code sample.

There are very likely free libraries online in PHP that allow isBot() functions to aid you in this.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
how do you think to track non-bot users? since many of the bots community can have a valid http agent of firefox or chrome,
only thing you can do - is to redirect by js - to be sure in condition. Bots will not be redirected at 70% rate. that's probably the best you can get.
Cause bots very often doesn't have a js engine.

#4
RHochstenbach

RHochstenbach

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
It might be a better idea to set up an .htaccess file to redirect known bot IP addresses.

#5
kylel

kylel

    Newbie

  • Members
  • Pip
  • 2 posts

Alexander said:

User agents are unique, you likely will have better luck doing the not equal to on the first mentioned code sample.

There are very likely free libraries online in PHP that allow isBot() functions to aid you in this.

Okay, so in essence I think you are suggesting I just make a bigger list of bots (or function to include more)? So please take a look at this function...


<?php


$bot_list = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",

"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",

"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",

"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",

"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",

"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",

"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler");


function detect_bot() {

global $bot_list;


foreach($bot_list as $bot) {

if(ereg($bot, $_SERVER['HTTP_USER_AGENT'])) {

$thebot = $bot;

}

}


if ($bot) {

return $thebot;

}

}

?>

I would save this as a php file on my website, then use this function to redirect:

include ("bot_detection.php");


if (!(bot_detection())) {

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: http://new-location.com");

	exit;

}

?> 

Would this work and include all the bots in my previous script? Thanks a ton:lol:

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You could certainly do this, although stripos is much faster to check than regular expressions of which load a library every time they are used.

Albeit, you could maybe throw these rules in to a htaccess or httpd.conf file as Apache would redirect before even starting up the PHP module and thus may be fastest even with regular expressions. If the speed is not different for you though, there is no reason to switch if it is harder to write.

RHochstenbach said:

It might be a better idea to set up an .htaccess file to redirect known bot IP addresses.

Bots use many IP addresses so that they can efficiently connect to multiple resources, that may be fruitless way to detect bots.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users