-1

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

Draken
  • 3,134
  • 13
  • 34
  • 54
  • 3
    You should look for a tutorial on URL rewriting – AntoineB Jul 20 '16 at 13:33
  • 1
    url rewriting is the correct answer, however note, that it does only work in one direction: the user enters "example.com/person/10" and the rewrite will change it to "example.com/person?id=10" – Honk der Hase Jul 20 '16 at 13:41
  • 1
    Possible duplicate of [URL rewriting with PHP](http://stackoverflow.com/questions/16388959/url-rewriting-with-php) – Draken Jul 20 '16 at 14:01

2 Answers2

0

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
Dharmendra
  • 127
  • 12
0

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;
    }
Poiz
  • 7,611
  • 2
  • 15
  • 17