I have been trying to create a simple router for a very simple website with PHP and apache2, but I keep getting 404 messages.
Before you go through this lengthy post, I did add DocumentRoot in nt2.conf in sites-available. I have not added it anywhere else.
I created a new directory called /var/www/nt2 (on Ubuntu 25.10). In there I created this
/var/www/nt2
index.php (contains the router)
.htaccess
views
home.php
contact.php
users.php
404.php
Apache runs fine. PHP has been installed successfully, I have version 8.4.11. I can get any page if I fill in the full path. For example : localhost/views/contact.php. That works, but localhost/contact does not work at all and produces a 404 error.
I'm quite the beginner, but this is not complicated code and I just don't see what I'm not doing right.
This is index.php, I copied it from an article.
<?php
$request = $_SERVER['REQUEST_URI'];
$viewDir = __DIR__ . '/views';
switch ($request) {
case '':
case '/':
require $viewDir . 'home.php';
break;
case '/users':
require . 'users.php';
break;
case '/contact':
require $viewDir . 'contact.php';
break;
default:
http_response_code(404);
require $viewDir . '404.php';
}
?\>
The files in the views folder are all like this:
<?php
echo "<h1>Contact me</h1>
<p>Email me: [email protected]</p>"
?>
Nothing fancy.
I adjusted apache2.conf:
I added the ServerName at the bottom to get rid of a message: (AH00558 - could not reliably determine the server's fully qualified domain.
ServerName
127.0.0.1
I also added this :
<Directory /var/www/nt2/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
I tried changing AllowOverride to All (I did the same thing for <Directory /var/www/> which exists in the default version. Nothing changed so I changed it back to None in both directories.
I created a configuration file in sites-available called nt2.conf and entered these commands: (The domain doesn't really exist btw.)
ServerName www.nt2.com
ServerAlias nt2
ServerAdmin webmaster@localhost
DocumentRoot /var/www/nt2
Then I ran:
a2ensite nt2.conf
a2dissite 000-default.conf
I also ran:
ufw allow http
ufw allow https # I don't remember why I did that.
ufw allow ssh
ufw allow 'Apache Full' # I found that in an article.
There's an .htaccess which I got from the same article.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I more or less understand what it means, but I did just copy it. I've also tried commenting everything out, but it makes no difference.
I ran a2enmod rewrite
In mods-available I made changes in userdir.conf
UserDir nt2
UserDir disabled root
<Directory /var/www/nt2>
AllowOverride All
Options Indexes FollowSymLinks
Require all granted
</Directory>
I ran a2enmod userdir :
I adjusted php.init and added /var/www/nt2 to include_path (I had an error message).
I looked at all the permissions. /var/www/nt2 is owned by me, $USER. The default folder html is owned by root. All files and directories inside the folder are owned by me.
I added myself to www-data group. (I read that somewhere.)
Nothing works. I'm at the end of my tether and I really cannot figure this out.
I commented out the following in mods-available/php8.4.conf
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>
I ran a2enmod php8.4
I'm not getting any other error messages. Just the 404.
I reloaded and restarted apache2 a million times.
I ran all the a2blabla commands again and again.
Nope.
I am so tired. I tried ChatGPT. It assured me that my router logic was wrong, gave me more complicated code. It doesn't work at all. I have the impression I don't know what the right question is because I don't know what I don't know. If I do know what I don't know, I can ask good questions and figure it out. I agree this is flimsy code, but I do intend to replace it with classes and a router.php and everything spic and span. I just feel if I understand this bit well, the more complicated stuff will be easier to digest.
www.nt2.comorlocalhost. Your config suggests the former, but you are usinglocalhost. Have you configured a local DNS server or HOSTS file?RewriteRule . /index.php \[L\]- Are those backslashes in your actual code or just erroneous characters in your question?sites-available(you've used the correct spelling later, so I assume this is just an error in your question).