Enabling mod_rewrite in Apache on Ubuntu
If you are running a LAMP stack on Ubuntu and need to activate mod_rewrite, the process is straightforward but often obscured by outdated tutorials targeting deprecated package versions. The core requirement is ensuring the Apache module is installed, enabled within the configuration hierarchy, and loaded at runtime. On modern Ubuntu distributions, the package manager handles the installation, but manual intervention is frequently required for the loading mechanism depending on your Apache version.
First, verify that the libapache2-mod-ruby or similar rewrite libraries are present, though typically the core module comes pre-installed with the Apache package. The critical step involves enabling the module using a2enmod. Run the following command in your terminal:
sudo a2enmod rewrite
This command updates the symbolic links in /etc/apache2/mods-enabled to point to the configuration files in /etc/apache2/mods-available. If this command returns successfully without error, the module is registered. However, registration does not guarantee that Apache will read the new settings immediately. You must restart or reload the web server to apply changes:
sudo service apache2 restart
On newer Ubuntu releases using systemd, you might prefer sudo systemctl restart apache2. Restarting clears any cached configurations and ensures the daemon picks up the newly enabled modules. If you encounter a "Module already loaded" error, it indicates the module is active but perhaps not configured correctly for your specific use case, such as enabling .htaccess support which is sometimes disabled by default on Ubuntu for security reasons.
A common pitfall occurs when developers assume that a2enmod rewrite is sufficient to allow URL rewriting in their .htaccess files or VirtualHost configurations. By default, many Ubuntu installations disable the reading of user configuration files via the AllowOverride directive. Without this, even a perfectly written .htaccess file will be ignored by Apache.
To resolve this, you must edit the virtual host configuration. Locate your site's configuration file in /etc/apache2/sites-available/, typically named after the domain or IP address (e.g., 000-default.conf). Open the file and ensure the <VirtualHost *:80> block contains:
<Directory /var/www/html>
AllowOverride All
</Directory>
If you are targeting a specific subdirectory, adjust the path accordingly. The AllowOverride All directive grants Apache permission to process directives like RewriteRule and RewriteCond found in .htaccess files or within the VirtualHost context. Without this line, Apache effectively strips out rewrite rules before processing the request, leading to 404 errors despite correct configuration.
Another frequent issue involves conflicting modules or SELinux/AppArmor restrictions, though AppArmor is less common on standard Ubuntu LAMP setups. If you have recently upgraded Apache or changed package versions, ensure that the rewrite module exists in /etc/apache2/mods-available/. If it is missing, you may need to install the specific module package:
sudo apt-get update
sudo apt-get install libapache2-mod-ruby libapache2-mod-ssl (if needed)
Note that on very old Ubuntu versions like 12.04, package repositories are no longer supported by default, so you might need to add third-party PPA sources or compile from source if the official repo lacks the update. However, for current LTS versions, the a2enmod approach remains the standard and most reliable method.
Once enabled and configured, test your setup by creating a simple .htaccess file in your document root with a rule to redirect traffic:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Accessing any non-existent file should now route to your application entry point, confirming that mod_rewrite is functioning. Remember that security considerations dictate keeping AllowOverride restrictive unless specifically needed for complex routing logic or CMS frameworks like Laravel.