Created
April 9, 2012 18:58
-
-
Save geirarne/2345525 to your computer and use it in GitHub Desktop.
url-tolking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RewriteEngine On | |
# legg inn unntak, som for eksempel for grafikk, javascript og css: | |
RewriteCond %{REQUEST_URI} ^/gfx.* | |
RewriteRule .* - [L] | |
RewriteCond %{REQUEST_URI} ^/js.* | |
RewriteRule .* - [L] | |
RewriteCond %{REQUEST_URI} ^/css.* | |
RewriteRule .* - [L] | |
# send alt annet til index.php | |
RewriteRule ^(.*)$ index.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// 1. KONFIGURERING | |
// | |
// definer hva slags innhold som finnes – slik at vi kan gi 404-feilmelding dersom det ikke finnes, | |
// uten å foreta en spørring til databasen med variabel fra URLen | |
function get_config($element) | |
{ | |
$modules = array('mod1','mod2','mod3'); // hvis det er mange, kan du hente disse fra databasen | |
$pages = array('page1','page2','page3'); // disse vil du sansynligvis hente fra databasen | |
$subpages = array('subpage1','subpage2','subpage3'); // disse vil du sansynligvis hente fra databasen | |
return $$element; | |
} | |
// | |
// 2. FUKSJONER | |
// | |
function clean_str($string) | |
{ | |
// legg til mysql_real_escape_string her - jeg har ikke gjort det, da det krever database-tilkobling | |
return htmlspecialchars(strip_tags($string)); | |
} | |
function get_path_array() | |
{ | |
// clean the request | |
$request = clean_str($_SERVER['REQUEST_URI']); | |
// fjern GET-variabler | |
if (strstr($request, '?')) : | |
$request = substr($request, 0, utf8_strpos($request, '?')); | |
endif; | |
// remove the first slash | |
$request = preg_replace('/^\/(.+)$/', "\\1", $request); | |
// remove optional last slash | |
if (ereg('^(.+)/$', $request)) : | |
$request = preg_replace('/^(.+)\/$/', "\\1", $request); | |
endif; | |
// split the path with slashes, if not at root | |
if ($request != '/') : | |
$path = explode('/', $request); | |
endif; | |
// remove empty path elements | |
foreach($path as $path_block) : | |
if (empty($path_block)) : | |
throw_404(); | |
endif; | |
endforeach; | |
return $path; | |
} | |
function throw_404() | |
{ | |
header("HTTP/1.1 404 Not Found"); | |
// du vil sikkert ha denne som en egen fil du inkluderer | |
?><html> | |
<body> | |
<h1>404 - siden ble ikke funnet</h1> | |
</body> | |
</html><?php | |
exit(); | |
} | |
function check_item($items, $path, $path_pos) | |
{ | |
if (isset($path[$path_pos]) && !empty($path[$path_pos])) : | |
if (in_array($path[$path_pos], $items)) : | |
return $path[$path_pos]; | |
else : | |
throw_404(); | |
endif; | |
else : | |
return false; | |
endif; | |
} | |
function check_page($path) | |
{ | |
return check_item(get_config('pages'), $path, 0); | |
} | |
function check_subpage($path) | |
{ | |
return check_item(get_config('subpages'), $path, 1); | |
} | |
function check_module($path) | |
{ | |
return check_item(get_config('modules'), $path, 2); | |
} | |
function bootstrap() | |
{ | |
// default-verdier | |
$data = array('page' => false, | |
'subpage' => false, | |
'mod' => false); | |
$path = get_path_array(); | |
// hvis forside | |
if (count($path) == 0) : | |
$data['page'] = 'frontpage'; | |
elseif (count($path) > 3) : // hvis mer enn tre parametre, vet vi det er feil | |
throw_404(); | |
else : // give 404 if param is set but non-exising | |
$data['page'] = check_page($path); | |
$data['subpage'] = check_subpage($path); | |
$data['mod'] = check_module($path); | |
endif; | |
return $data; | |
} | |
// | |
// 3. SETT I GANG APPLIKASJONEN | |
// | |
$data = bootstrap(); | |
// | |
// 4. PRINT UT HTML | |
// | |
?> | |
<html> | |
<body> | |
<table> | |
<tr> | |
<td> | |
<strong> | |
page | |
</strong> | |
</td> | |
<td> | |
<?php echo $data['page'] ? $data['page'] : 'NOT SET'; ?> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<strong> | |
subpage | |
</strong> | |
</td> | |
<td> | |
<?php echo $data['subpage'] ? $data['subpage'] : 'NOT SET'; ?> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<strong> | |
mod | |
</strong> | |
</td> | |
<td> | |
<?php echo $data['mod'] ? $data['mod'] : 'NOT SET'; ?> | |
</td> | |
</tr> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment