File: D:/HostingSpaces/SBogers10/lab.komma-mediadesign.nl/wwwroot/lab/config/friendlyUrl.php
<?php
/**
* friendlyUrl.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/19/13
*/
/**
* Following code is used to build user-friendly URLs.
* 1. Set if we have a language in the url.
*/
$addLanguage = false;
/**
* 2. Get the REQUEST_URI and explode this on '/'
*/
$stringUrl = substr($_SERVER['REQUEST_URI'],1);
$urls = explode('/', $stringUrl);
/**
* 3. If we have a language, the structure will be root.com/lang/page/sub/sub2/sub3
* Check if the lang is valid, else header 404.
* If no language is set, header to "nl".
*/
$urlCount = 0;
if($addLanguage)
{
$languages = array('nl','en');
$default = $languages[0];
if(isset($urls[$urlCount]) && ! empty($urls[$urlCount]))
{
$lang = trim(urldecode($urls[$urlCount]));
if(in_array($lang,$languages))
{
define('URL_LANG', $lang);
}
else{
header("HTTP/1.0 404 Not Found");
header('location: /'.$default.'/404');
exit;
}
}
else{
// redirect to default page
header ('HTTP/1.1 301 Moved Permanently');
header('Location: /'.$default.'/');
exit;
}
$urlCount++;
}
/**
* 4. The second - or first if ! $addLanguage - GET-variable is $page
*/
if(isset($urls[$urlCount]) && ! empty($urls[$urlCount]))
{
$page = trim(urldecode($urls[$urlCount]));
if(!empty($page))
{
define('URL_PAGE', $page);
$urlCount++;
}
}
/**
* 5. All the other get variables are called sub, sub2, sub3 etc.
*/
if(count($urls) > $urlCount)
{
for($i=$urlCount;$i<=count($urls);$i++)
{
if(isset($urls[$i]))
{
$value = trim(urldecode($urls[$i]));
$name = 'URL_SUB';
if($i > $urlCount) $name.= $i;
if($value != '')
{
define($name,$value);
}
}
}
}