Skip to content

Instantly share code, notes, and snippets.

@qutek
Last active February 2, 2020 12:09
Show Gist options
  • Save qutek/c4588bfa4b655d798b36 to your computer and use it in GitHub Desktop.
Save qutek/c4588bfa4b655d798b36 to your computer and use it in GitHub Desktop.
[PHP][htaccess] Parse path from Clean URL (Prety Permalink)
<?php
/**
* parse path to get from clean URL
* @return [array] [array of requested uri]
*/
function parse_path() {
$path = array();
if (isset($_SERVER['REQUEST_URI'])) {
$request_path = explode('?', $_SERVER['REQUEST_URI']);
$path['base'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/');
$path['call_utf8'] = substr(urldecode($request_path[0]), strlen($path['base']) + 1);
$path['call'] = utf8_decode($path['call_utf8']);
if ($path['call'] == basename($_SERVER['PHP_SELF'])) {
$path['call'] = '';
}
$path['call_parts'] = explode('/', $path['call']);
$path['query_utf8'] = urldecode(@$request_path[1]);
$path['query'] = utf8_decode(urldecode(@$request_path[1]));
$vars = explode('&', $path['query']);
foreach ($vars as $var) {
$t = explode('=', $var);
$path['query_vars'][@$t[0]] = @$t[1];
}
}
return $path;
}
$path_info = parse_path();
echo '<pre>'.print_r($path_info, true).'</pre>';
/*
Enable rewrite use this .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
*/
/* Sample request URI
http://localhost/user/matthew/edit?language=en&hobbies=art&sport=football
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment