How to Fix Apache 404 Not Found on Debian 11
-
The Root Cause: On Debian 11, “Apache 404 Not Found” errors frequently occur because the
mod_rewritemodule, essential for handling “pretty URLs” in many modern web applications, is not enabled by default. Additionally, Apache’s defaultAllowOverride Nonedirective in its configuration can prevent.htaccessfiles (which commonly contain URL rewrite rules) from being processed. -
Quick Fix (CLI): This quick fix addresses the scenario where
mod_rewriteis not enabled.sudo a2enmod rewrite sudo systemctl restart apache2 -
Configuration Check: If the Quick Fix doesn’t resolve the 404, the issue likely lies in Apache’s inability to process
.htaccessfiles, or an incorrectDocumentRootsetting.-
Verify DocumentRoot: Edit your virtual host configuration file, typically found at
/etc/apache2/sites-available/your-site.conf(replaceyour-site.confwith your actual site’s file, e.g.,000-default.conf). Ensure theDocumentRootdirective accurately points to the directory containing your website’s files.# Example: /etc/apache2/sites-available/your-site.conf <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/your-application-directory # <-- Confirm this path ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> -
Enable .htaccess processing (AllowOverride): Within your virtual host configuration file (
/etc/apache2/sites-available/your-site.conf), locate or add a<Directory>block that matches yourDocumentRoot. Inside this block, changeAllowOverride NonetoAllowOverride All. This enables.htaccessfiles for that directory.# Example: /etc/apache2/sites-available/your-site.conf <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/your-application-directory <Directory /var/www/html/your-application-directory> # <-- Match your DocumentRoot Options Indexes FollowSymLinks AllowOverride All # <-- Change this from None to All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>After making changes, always restart Apache to apply them:
sudo systemctl restart apache2
-
-
Verification: After applying any fixes, confirm Apache’s status and attempt to access the URL that was previously returning a 404.
sudo systemctl status apache2 curl -I http://your-domain.com/path/to/resourceA successful fix will display an HTTP status code other than
404 Not Found(e.g.,200 OKor301 Moved Permanently).