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;
}
}
CodePudding user response:
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;
}
CodePudding user response:
I found a solution with .htaccess with this code
RewriteCond %{QUERY_STRING} ^page=[^&] &?(.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1 [R=302,L]
