I recently learned how to clean my url by removing the ".php" or some other post and get variables in url, which is good. But then I'm having a problem with the landing page. Whenever I enter my website's url say for example: https://example.com, it should take me to https://example.com/index which is the landing page in this case. I can't seem to get it right.
I tried changing my .htaccess file but didn't work and the code left there in .htaccess is just this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]
And in my router.php file I have this bunch of code which cleans up the url. <?php
function split_url(){
$url = isset($_GET['url']) ? $_GET['url'] : "index";
$url = explode("/", filter_var(trim($url,"/"),FILTER_SANITIZE_URL));
return $url;
}
$root= $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME']. $_SERVER['PHP_SELF'];
$root = trim(str_replace("router.php","",$root), "/");
define("ROOT", $root . "/");
$URL = split_url();
if(file_exists($URL[0].".php")){
require($URL[0] . ".php");
}
else{
require("404.php");
}
The problem is, when I go search my site's url ( https://example.com) it loads the page but it's empty and only if I search ( https://example.com/index) it takes me to the page with everything in it. Please help me with this if there is another way of doing it.
It looks like you just need to set the
DirectoryIndextorouter.php. (TheDirectoryIndexsets the file to serve when a directory is requested.) Because of the second condition (RewriteConddirective) your existing mod_rewrite rule doesn't do anything for requests to the root directory (or any directory for that matter). This is why many front-controllers useindex.php, which is the defaultDirectoryIndexdocument.For example, at the top of your
.htaccessfile:The above will result in
router.phpbeing looked for in any directory that is requested. So, request the root, it will look forrouter.phpin the root. You could change this to a root-relative path that it will always serve/router.php(from the root directory) for whatever directory is requested. ie.DirectoryIndex /router.php.It looks like your PHP script should then default to
index.