0

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:

  1. When I try the default path www.myWebPageName.com/app/, page1 is rendered successfully
  2. When I try on the servers using www.myWebPageName.com/app/page1, I get NOT FOUND
apoorva
  • 15
  • 3

1 Answers1

0

You wouldn't need to include /project_folder/app/ in the RewriteRule since you are already using a RewriteBase.

RewriteEngine On
RewriteBase /app/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,QSA]

The way you have it written is the same as saying (without the RewriteBase):

RewriteRule . /app/project_folder/app/index.php [L,QSA]

The top answer here gives a pretty good explanation of what you're trying to achieve: How does RewriteBase work in .htaccess

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62