slothinspace slothinspace - 2 months ago 6x
PHP Question

How to load up pages in different directory without it affecting URL

My file structure usually goes something like this -


etc ...

After basically creating more and more pages, its becoming much more difficult to manage them because it's essentially turned the root directory into a massive list of pages.

How can I make it so the pages are placed in a folder along with its associated category like this -


without it affecting the URL so it still looks like

Will editing the .htaccess be the most effective method of doing this? or is there another way?


I created a singular php file in the root directory that basically got a PHP page from a $_GET request and then checked to see if the PHP page exists. If it did then I used REQUIRE_ONCE to get the page.

//Get page from $_GET request 
if (isset($_GET['page'])){
  $page = $_GET['page'];

//Create path including the page
$url = ROOT_DIR . "/pages/".$page;

//If the page exists include it, if not then show a 404 page.
  REQUIRE_ONCE "/pages/404.php";

This allows me to access login.php in the /pages/ directory using

I then just rewrote the URL with .htaccess to make it more attractive.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1.php [QSA,L]

so that allows me to use