lafif-a
10/3/2014 - 7:34 PM

[PHP][htaccess] Parse path from Clean URL (Prety Permalink)

[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
 */
?>