Enabling mod_rewrite unlocks clean URLs, canonical redirects, and front-controller routing in Apache for applications that rely on RewriteRule directives. When the module is disabled, rewrite directives are ignored, commonly surfacing as unexpected 404s, missing “pretty” paths, or frameworks failing to route requests correctly.
Apache implements features through loadable modules, and mod_rewrite plugs into the request processing pipeline to transform URLs before content is served. Rewrite rules can be defined centrally (server config or VirtualHost) or per-directory using .htaccess, with .htaccess only being evaluated when AllowOverride permits it for that directory tree.
On Ubuntu or Debian, enabling mod_rewrite is typically done using a2enmod and applied by restarting the apache2 service. Allowing .htaccess overrides is powerful but increases per-request filesystem checks and broadens the configuration surface, so constraining overrides to the smallest directory plus the minimum class (FileInfo for rewrites) reduces risk.
Related: How to enable or disable Apache modules
Related: How to test your Apache configuration
Steps to enable mod_rewrite in Apache:
- Show the active VirtualHost mapping to identify the site configuration file, including the DocumentRoot.
$ sudo apache2ctl -S VirtualHost configuration: *:80 host.example.net (/etc/apache2/sites-enabled/000-default.conf:1) ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/var/log/apache2/error.log" ##### snipped #####
- Enable mod_rewrite with a2enmod.
$ sudo a2enmod rewrite Module rewrite already enabled
On RHEL-derived systems, module loading is controlled by LoadModule directives (commonly under /etc/httpd/conf.modules.d), and the service name is typically httpd.
- Open the target site configuration file for editing.
$ sudoedit /etc/apache2/sites-available/000-default.conf
When the active file is under /etc/apache2/sites-enabled, editing the matching /etc/apache2/sites-available file keeps changes consistent with the a2ensite workflow.
- Add a Directory block that permits rewrite directives from .htaccess under the DocumentRoot.
<Directory /var/www/html> Options FollowSymLinks AllowOverride FileInfo Require all granted </Directory>AllowOverride FileInfo enables rewrite-related directives; use AllowOverride All only when the .htaccess also requires other override classes (for example Options or AuthConfig).
- Test the Apache configuration syntax.
$ sudo apache2ctl configtest Syntax OK
- Restart the apache2 service to load the module, applying configuration changes.
$ sudo systemctl restart apache2
A graceful reload can reduce disruption on busy hosts:
$ sudo systemctl reload apache2
- Confirm rewrite_module is loaded.
$ sudo apache2ctl -M | grep rewrite rewrite_module (shared)
- Create a temporary test directory under the DocumentRoot.
$ sudo mkdir -p /var/www/html/rewrite-test
- Create an .htaccess file in the test directory with a simple redirect rule.
$ sudo tee /var/www/html/rewrite-test/.htaccess >/dev/null <<'EOF' RewriteEngine On RewriteRule ^$ / [R=302,L] EOF
Placing rewrite rules directly in the site config (inside <VirtualHost>) avoids .htaccess overhead and does not require AllowOverride.
- Request the test URL to confirm a redirect response.
$ curl -sI http://localhost/rewrite-test/ HTTP/1.1 302 Found Date: Sat, 10 Jan 2026 05:06:23 GMT Server: Apache/2.4.58 (Ubuntu) Location: http://localhost/ Content-Type: text/html; charset=iso-8859-1
- Remove the temporary test directory after verification.
$ sudo rm -rf /var/www/html/rewrite-test
Removing the wrong path deletes content; verify the target directory matches the intended test location before running a recursive remove.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
