Connecting Tech Pros Worldwide Help | Site Map

301 redirect headers / restructured site

Mike P
Guest
 
Posts: n/a
#1: Sep 28 '05
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&Itemi d=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."&Item id=".$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=".$u rlitemid;

// 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");

}
?>


R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#2: Oct 2 '05

re: 301 redirect headers / restructured site


Mike P wrote:
<snip>[color=blue]
> // 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);[/color]

I suppose, you're not a newbie. But, the include_once sytax is
broken; you don't have to use "http://", if used, it will be have
differently.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Closed Thread