Search engine friendly URL problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rcity
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2004 2:21 pm

Search engine friendly URL problem

Post by rcity »

Hi,

I need to make the URL for my site more friendly to search engines. Does anyone know what the problem with this code, or server settings are, or where I can find similar code for PHP running on a w2k server?

I found a nice example in "Core PHP Programming" for putting GET variables between slashes. The example worked fine on my test server after changing the $_SERVER variables used, to ones that would work under Windows, but didn't work on the production server. After upgrading the test server to PHP ver. 4.3.4 from 4.3.1, the example file had the same problem.

The example file generates a random number that it uses as a link to the next page. This works fine and displays something like:

View Message 703

After clicking on "View Message 703" the next page a “404 page not found error”, instead of the next random number. The current code follows and this is running on:
Server: W2k
PHP: 4.3.4

Code: Select all

<?php

	$path82 = $_SERVER['REQUEST_URI'];
	echo $path82.'is<br>';

	if(isset($_SERVER['REQUEST_URI']))
	{
		//remove .html from the end
		$path = str_replace(".html", "", $_SERVER['REQUEST_URI']);

		//remove leading slash
		$path = substr($path, 1);

		//iterate over parts
		$pathVar = array();
		$v = explode("/", $path);
		$c = count($v);
		for($i=0; $i<$c; $i += 2)
		{
			$pathVar[($v[$i])] = $v[$i+1];
		}
		print("You are viewing message " .
			"{$pathVar['message']}<br>\n");
	}

	//pick a random ID
	$nextID = rand(1, 1000);

	print("<a href="{$_SERVER['PHP_SELF']}/message/$nextID.html">" .
		"View Message $nextID</a><br>\n");
?>

Thanks
?>
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

After reading this article I wrote a function that does what you are trying to do. Take a look at it:

Code: Select all

<?php
/**
 * @return ARRAY
 * @desc Turns path info into something similar to $_GET. For search-engine friendly URL's.
 * @date 12-20-03
 */
function fetch_path() {
	$_PATH = array();
	if( isset($_SERVER['PATH_INFO']) ) {
		$getdata = explode('/', $_SERVER['PATH_INFO']);
		$getcount = count($getdata);
		if( $getcount % 2 == 0 ) {
			$getdata[] = '';
			$getcount++;
		}
		for( $i = 1; $i < $getcount; $i += 2) {
			$_PATH[$getdata[$i]] = $getdata[$i+1];
		}
	}
	return $_PATH;
}
?>
so now, if you are calling the page test.php/id/304/some/thing, you can use the function like this: $path = fetch_path(); And the array will look like this:

Code: Select all

$path = array(
    'id' => 304,
    'some' => 'thing'
);
It's worked fine on my win2k test server.
Post Reply