Web Hosting Talk







View Full Version : Need script: if URL contains ? then do this


PageUp2
10-30-2005, 12:22 PM
I need a script that would add "<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">" to the page if the URL contains a question mark (?). Is this possible and what would the syntax be?

alafoo
10-30-2005, 12:48 PM
One way to do it:


$path = $_SERVER['REQUEST_URI'];
$pos = stripos($path, "?");
if ($pos>0) echo "there was a ? in the requested URL";

Dan L
10-30-2005, 01:56 PM
Here's a few other methods.. if($_SERVER['QUERY_STRING'] != '') {
return true;
} if(count($_POST) > 0) {
return true;
}They both should work.

error404
10-30-2005, 04:51 PM
Here's a few other methods.. if($_SERVER['QUERY_STRING'] != '') {
return true;
} if(count($_POST) > 0) {
return true;
}They both should work.

The second won't ;). You want count($_GET) instead :).

Dan L
10-30-2005, 05:05 PM
The second won't ;). You want count($_GET) instead :).I was thinking of that as I was writing the post, but forgot by the time I hit submit. Good call :)

PageUp2
10-30-2005, 06:50 PM
Thanks guys for the replies but I'm afraid it's a bit more complicated than I first imagined.

1) When I tried alafoo's script, it gave me this error:

Notice: Undefined index: REQUEST_URI in C:\ServInt\vbulletin\test.php on line 2

Fatal error: Call to undefined function: stripos() in C:\ServInt\vbulletin\test.php on line 3

2) Then I tried with this:

<?php
if(count($_GET) > 0) {
echo "there was a ? in the requested URL";
}
?>
... and it worked (tried on a blank page). However, when I tried it on my vBulletin forum's showthread pages where I actually need this script, it didn't work properly - it gave "there was a ? in the requested URL" if that was true or not; probably because I use mod_rewrite.

For instance, URLs like
/showthread.php?t=123 get rewrited to
/thread123.html and URLs like
/showthread.php?t=123&pp=10 get rewrited to
/thread123.html?pp=10.

The only time the 2nd script didn't show "there was a ? in the requested URL" is when I tried to open /showthread.php (without any query strings) page.

Anyway, the point of this script is to add robots nofollow meta tag to rewrited URLs with ? (like /thread123.html?pp=10).

Anyone? :)

Dan L
10-30-2005, 07:10 PM
PHP only see the first URLs, not the rewritten ones.

As far as I remember, vB points all robots to the archive pages, so you shouldn't have an issue with them. I'd look around their forums for more information, but you shouldn't need to worry about this :)

PageUp2
10-30-2005, 08:09 PM
As far as I remember, vB points all robots to the archive pages, so you shouldn't have an issue with them.
Yeah, I wish that would be true. Google is all over the site, it crawls all kinds of URLs so that we get penalized for double content. Our referrals from Google dipped about 10x times in the last 6 months.

Thanks everyone for your help, I'll try to resolve this differently.

phxhtml
11-23-2005, 06:11 PM
One way to do it:


$path = $_SERVER['REQUEST_URI'];
$pos = stripos($path, "?");
if ($pos>0) echo "there was a ? in the requested URL";

Hi,

I am trying to get create condition statement:
If the URL conatains a certain word (ie. Case_Studies) then perform an action. But it does not work for me.'

Is there a reserved variable to get the directory "Case_Studies" in the following domain: domain.com/Case_Studies/index.php so that I can do conditions upon the directory name?

If I just use ($theuri = $_SERVER['REQUEST_URI']) it returns the directory AND the file with the scriptl; Case_Studies/index.php and I would have to run a condition statement on all files within the Case_Studies directory.

Thanks,
Mark

I tried with this:

<?php
switch ($theuri = $_SERVER['REQUEST_URI']):
$pos = stripos($theuri, "Case_Studies");
case ($pos>0);
echo $itemis = "<img src=\"images/arrow.gif\" alt=\"arrow \">
<span class=\"note\"> CASE STUDIES </span>";
break;
default:
echo "NOBODY IS HOME";
endswitch;
?>

PageUp2
11-23-2005, 06:27 PM
I'm not sure if it helps you, but on my forum, I use this:

ob_start();
//print_r($_GET);
$matchingArr["t"]="set";
$matchingArr["page"]="set";
$matchingArr["threadid"]="set";
$matchingArr["pagenumber"]="set";
$matchingArr["f"]="set";
$matchingArr["forumid"]="set";
////////////////////////////////
$bDisplayMeta = 0;
foreach ($_GET as $key1 => $myKey)
{
$bOk = 0;
foreach ($matchingArr as $key2 => $myKey2)
if ($key1==$key2)
$bOk=1;
if ($bOk==0)
$bDisplayMeta = 1;
}

if($bDisplayMeta) {
echo "<META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\" />";
}
$meta_robots = ob_get_contents();
ob_end_clean();

I end up paying someone to do this script for me. :)