php: parsing search engine friendly url

by prettyscripts on 2008-02-28 16:40

php

a typical url looks like this:

some_domain/index.php?section=about&content=me

a search engine friendly url looks like this:

some_domain/index.php/about/me

to get the ‘/about/me’ part of the url, it’s returned by standard php variable $_SERVER[’PATH_INFO’].

to parse it into parts:

PHP:

list($dummy$section$content) = explode("/"$_SERVER['PATH_INFO']);

explode splits the string into array delimited by “/". because the string starts with “/", the first array element will not have a value. hence it’s assigned to $dummy.

ideally, the url should be without ‘index.php’ and look like this:

some_domain/about/me

.htaccess file must be setup for this to work. or else a 404 error returns since it’s trying to look up non-existing subdirectories.