You are here: 301 redirect headers / restructured site « PHP Programming Language « IT news, forums, messages
301 redirect headers / restructured site

Posted by Mike P on 09/29/05 00:06

I'm using MamboServer for my site, and have switched from SEF URLs to
standard URLs. Of course, I'm indexed everywhere with the SEF urls. I'm
trying to write a script, so that users and search engines can find the
pages using the new url. My goal is, that if I can decode the URL they're
requesting, I'll send them a 301 header and redirect them to the new URL.
Otherwise, I'll send them a 404 error with a message. It's not working, all
I get is a regular 404 page every time, can you help?

Here's my code:

<?php

// trying to change the url to the new page, send 301 header and redirect if
I can, if not, send them a 404 with a customized error page
// I've put an "ErrorDocument 404" line in my .htaccess file to get me to
this script... I don't think apache sends a 404 header at this point.
//
// I'm getting two types of requested uris:
// "content" and "components"
// old "content" uri's look like this: /content/view/11/22/
// they should look like this:
/index.php?option=com_content&task=view&id=11&Itemid=22
// old "component" uri's look like this:
/component/option,com_weblinks/task,view/catid,88/id,99/
// they should look like this:
/index.php?option=com_weblinks&catid=88&Itemid=99

// Get the URL they requested, open it up and see if it's looking for
content or a component.

$requri = getenv ("REQUEST_URI");
$urlarray = explode("/", $requri , 7);
$urltype = $urlarray[1];
$baseurl = "http://mysite.com/index.php?option=";

// Check the URL to see if they're looking for a "component" or "content"

if ($urltype == "content") {
// it's "content".
// extract the "id" and "Itemid"
$urlid = $urlarray[3];
$urlitemid = $urlarray[4];

// form the new url
$newloc =
$baseurl."com_content&task=view&id=".$urlid."&Itemid=".$urlitemid;

// notify administrator - this is a simple mail(), letting me know what
happened, the requesturi and where I sent them, no html or header() there.
include_once(http://mysite.com/error_pages/inc/errorpage.inc);

// send the 301 header
header("HTTP/1.0 301 Moved Permanently");
header("Location: ".$newloc);

}
elseif ($urltype == "component") {
// it's a "component".
// extract the component name, catid and itemid
$optionarray = explode(",", $urlarray[2], 2);
$urlcom = $optionarray[1];
$optionarray = explode(",", $urlarray[4], 2);
$urlcatid = $optionarray[1];
$optionarray = explode(",", $urlarray[5], 2);
$urlid = $optionarray[1];

// form the new url
$newloc = $baseurl.$urlcom."&catid=".$urlcatid."&Itemid=".$urlitemid;

// notify administrator - this is a simple mail(), letting me know what
happened, the requesturi and where I sent them, no html or header() there.
include_once(http://mysite.com/error_pages/inc/errorpage.inc);

// send the 301 header
header("HTTP/1.0 301 Moved Permanently");
header("Location: ".$newloc);
}
else {
// not sure what it is, so just show a 404 page
// notify administrator - this is a simple mail(), letting me know what
happened, the requesturi and where I sent them, no html or header() there.
include_once(http://mysite.com/error_pages/inc/errorpage.inc);

// send the 404 header
header("HTTP/1.0 404 Not Found");
header("Location: http://mysite.com");

}
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация