1

Greetings to all I have my URL http://localhost/?page= and when I have that I just want http://localhost/ Because it's loading the same home page for that URL but I don't want to show this "/?page=" in URL. How can I do that? Because I'm trying to create some MVC in my PHP folder structure

public function __invoke(){
    $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : "home";

    switch ($page) {
        case "home":
            $title = "Home of APP";

            require __DIR__."/../views/includes/main-header.php"; 
                require __DIR__."/../views/home.php"; 
            require __DIR__."/../views/includes/main-footer.php";
        break;

        case "insertCategories":
            $this->category_sub_category_questions->insertCategories("Sitewide Creative");
            $this->category_sub_category_questions->insertCategories("Global Elements");
            $this->category_sub_category_questions->insertCategories("Core Ecommerce Pages");
            $this->category_sub_category_questions->insertCategories("Advanced Ecommerce Features");
            $this->category_sub_category_questions->insertCategories("Brand Pages");
            $this->category_sub_category_questions->insertCategories("Development Environment Setup");
            $this->category_sub_category_questions->insertCategories("Launch and Post-launch");
            $this->category_sub_category_questions->insertCategories("Measurement and Conversion Optimization");
            $this->category_sub_category_questions->insertCategories("Consulting Services");

            echo "Categories Inserted!";
        break;

        default:
            require __DIR__."/../views/errors/404.php"; 
        break;
    }
}
Stefan Momcilovic
  • 574
  • 1
  • 6
  • 25
  • Does this answer your question? [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Markus AO Jan 28 '22 at 13:13

2 Answers2

1

You can try something like this

$url = 'http://localhost/?page=';
echo $this->cleanUrl($url);

/**
 * Clear URL remove empty query strings from it.
 * @Input URL
 * @Return Cleaned URL without empty query strings

*/
public function cleanUrl($url){
    $params = parse_url($url);
    parse_str($params['query'], $query);
    // Remove empty
    $cleanedParams = array_filter($query);
    
    $url = strtok($url, '?');
    $url = rtrim($url, '/');
    if(!empty($cleanedParams)){
        $url = $url.'?'.http_build_query($cleanedParams);
    }
    return $url; 
}
Ishaque Javed
  • 406
  • 4
  • 14
1

I found a solution with .htaccess with this code

RewriteCond %{QUERY_STRING} ^page=[^&]+&?(.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1 [R=302,L]
Stefan Momcilovic
  • 574
  • 1
  • 6
  • 25