How to Fix Apache 404 Not Found on Debian 11


  1. The Root Cause: On Debian 11, “Apache 404 Not Found” errors frequently occur because the mod_rewrite module, essential for handling “pretty URLs” in many modern web applications, is not enabled by default. Additionally, Apache’s default AllowOverride None directive in its configuration can prevent .htaccess files (which commonly contain URL rewrite rules) from being processed.

  2. Quick Fix (CLI): This quick fix addresses the scenario where mod_rewrite is not enabled.

    sudo a2enmod rewrite
    sudo systemctl restart apache2
  3. Configuration Check: If the Quick Fix doesn’t resolve the 404, the issue likely lies in Apache’s inability to process .htaccess files, or an incorrect DocumentRoot setting.

    1. Verify DocumentRoot: Edit your virtual host configuration file, typically found at /etc/apache2/sites-available/your-site.conf (replace your-site.conf with your actual site’s file, e.g., 000-default.conf). Ensure the DocumentRoot directive 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>
    2. 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 your DocumentRoot. Inside this block, change AllowOverride None to AllowOverride All. This enables .htaccess files 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
  4. 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/resource

    A successful fix will display an HTTP status code other than 404 Not Found (e.g., 200 OK or 301 Moved Permanently).