Forum Moderators: coopster

Message Too Old, No Replies

301 Redirect in PHP

Format to use based on url?

         

soquinn

6:23 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Hello, we're attempting to write a 301 redirect for 2 categories within a directory script template so not to incur duplicate content penalties for the two locations of the same content. Is there a way to write something like, if the category url is:

http*//www.foo.com/dir.php?foo1/foo2/foo3/foo4/

go to

http*//www.foo.com/foo4/

and everything under foo4/ would also redirect to the new location. I don’t think it can be done in .htaccess because the folders are not really there to contain the .htaccess file because they been re-written from something like:

http*//www.foo.com/dir.php?a=foo1&b=foo2&c=foo3

So is there a way in php to write it based on the url, maybe:

<?
if ($_SERVER['REQUEST_URI']) = http*//www.foo.com/foo1/foo2/foo3/foo4/){
header("HTTP/1.1 301 Moved Permanently");
header("Location: http*//www.foo.com/foo4/");
exit();
?>

Thanks

Spudstr

7:11 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



<?
if ($_SERVER['REQUEST_URI']) = http*//www.foo.com/foo1/foo2/foo3/foo4/){
header("HTTP/1.1 301 Moved Permanently");
header("Location: http*//www.foo.com/foo4/");
exit();
?>

yeah you can get away with somthing like that... check out eregi using regex.


if(eregi("http:\/\/([a-zA-Z0-9\.]+).com\/([a-zA-Z0-9\/]+)\/foo4",$_SERVER['REQUEST_URI'],$plist))
{
// do stuff here/redirect headers in here
}

$plist becomes an array you can do a foreach statement and see what elements print/contain what


foreach($plist as $k=>$l)
{
echo "Position: $k contains: $l<br>";
}

i might have the k and l in wrong spots for the wording.. but you get the idea of what spots in the array contain what.. also check the regex.. it should work but double check that i escaped things i needed and you are searching for what your looking for

soquinn

9:44 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Thanks Spudstr, I think I see what you are saying but I’m a bit of a php novice… can you recommend a good resource with a few code examples where I can learn more about of what you are suggesting?

Thanks