Last active
February 2, 2020 12:09
-
-
Save qutek/c4588bfa4b655d798b36 to your computer and use it in GitHub Desktop.
[PHP][htaccess] Parse path from Clean URL (Prety Permalink)
This file contains hidden or 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 | |
/** | |
* 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