I have this URL
http://www.mywebsite.com/person?id=10
but I don't want the $_GET
Variable.
I want it like so:
http://www.mywebsite.com/person/10
I have this URL
http://www.mywebsite.com/person?id=10
but I don't want the $_GET
Variable.
I want it like so:
http://www.mywebsite.com/person/10
You can use $_SERVER['REQUEST_URI'] and you can explode from "person" (if this will be fix)
$uri_parts = explode('person', $_SERVER['REQUEST_URI'], 2);
echo $uri_parts[1]; // will return /10
Below is a handy, little function you can use in such situations. You might want to test the code here.
<?php
$currentURL = getCurrentPageURL(); //<= GET THE ACTIVE PAGE URL
$cleanURL = getPreFormattedURI($currentURL);
var_dump($cleanURL);
// FUNCTION TO AUTOMATICALLY GET THE ACTIVE PAGE URL
function getCurrentPageURL() {
$pageURL = 'http';
if ((isset($_SERVER["HTTPS"])) && ($_SERVER["HTTPS"] == "on")) {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
}else {
$pageURL .= $_SERVER["SERVER_NAME"];
}
$pageURL .= $_SERVER["REQUEST_URI"];
return $pageURL;
}
// FUNCTION THAT FORMATS THE URL THE WAY YOU SPECIFIED
function getPreFormattedURI($uri, $key="id"){
$objStripped = new stdClass();
$objParsedQuery = new stdClass();
if(!stristr($uri, "?")){
$objStripped->M = $uri;
$objStripped->Q = null;
}else{
$arrSplit = preg_split("#\?#", $uri);
$objStripped->M = $arrSplit[0];
$objStripped->Q = $arrSplit[1];
}
$cleanURL = $objStripped->M;
if($objStripped->Q){
$arrSplit = preg_split("#[\?\&]#", $objStripped->Q);
if(!empty($arrSplit) && count($arrSplit)>0 ) {
foreach ($arrSplit as $queryKVPair) {
preg_match("#(.*)(\=)(.*)#", $queryKVPair, $matches);
list($fullNull, $key, $null, $value) = $matches;
$objParsedQuery->$key = $value;
$cleanURL .= "/" . $value;
}
}
$objStripped->Q = $objParsedQuery;
}
return $cleanURL;
}