I am trying to create a PHP router for a website with 2 pages on an Apache Server. But I keep running into NOT FOUND The requested URL was not found on this server
. Below is my code structure.
Folder Structure
/var/www/html/project_folder
| .htaccess
| app
| index.php
| page1.php
| page2.php
| 404.php
index.php
<?php
$request_uri = explode('/', parse_url($_SERVER['REQUEST_URI']));
$requested_page = $request_uri[2];
if ($requested_page === '' || $requested_page === 'page1') {
$route = '/page1.php';}
elseif ($requested_page === 'page2') {
$route = '/page2.php';
} else {
$route = '/404.php';
}
require __DIR__ . $route;
page1.php / page2.php - similar content
<?php
echo '<h1>Page No: 1</h1>';
.htaccess
RewriteEngine On
RewriteBase /app/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /project_folder/app/index.php [L,QSA]
When I run the same setup on localhost localhost/project_folder/app
and localhost/project_folder/app/page2
, the pages are rendered.
However, on the server:
- When I try the default path
www.myWebPageName.com/app/
, page1 is rendered successfully - When I try on the servers using
www.myWebPageName.com/app/page1
, I getNOT FOUND