Enable and Verify Apache 2.4 mod_rewrite for URL Rewriting
Learn how to enable Apache 2.4’s mod_rewrite, write URL‑rewrite rules, verify them with curl and logs, and recover if something breaks.
27 Aug 2026, 02:27 UTC

Desired Outcome
The goal is to have Apache 2.4 rewrite incoming URLs according to rules you define, ensuring redirects or internal rewrites work as expected. Once configured, a request to http://example.com/oldpage.html should return a 301 redirect to http://example.com/newpage.html (or rewrite internally to a different resource).
Prerequisites
- Apache 2.4 installed and running (e.g.,
httpdon RHEL/CentOS,apache2on Debian/Ubuntu). - Root or sudo privileges to edit configuration files and restart the service.
- Access to the server’s file system (SSH or console).
- Basic knowledge of Apache configuration syntax.
Enabling mod_rewrite
mod_rewrite is bundled with Apache 2.4 but may not be loaded by default. To enable it, edit the main configuration file (often /etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf) and ensure the following line exists and is not commented out:
LoadModule rewrite_module modules/mod_rewrite.so
On Debian/Ubuntu, the module is usually already loaded via mods-enabled/rewrite.load.
After editing, reload the module list by restarting Apache:
sudo systemctl restart apache2 # Debian/Ubuntu
sudo systemctl restart httpd # RHEL/CentOS
Configuring Rewrite Rules
Rewrite rules can be placed either in a .htaccess file inside a directory or directly inside a <VirtualHost> block. The choice depends on your deployment model. For this guide, we’ll use a VirtualHost example for clarity.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
# Enable the rewrite engine
RewriteEngine On
# Example: Redirect oldpage.html to newpage.html
RewriteRule ^/oldpage\.html$ /newpage.html [R=301,L]
# Example: Internally rewrite /blog/post to /index.php?page=post
RewriteRule ^/blog/(.*)$ /index.php?page=$1 [L]
# Optional: Log rewrite activity for debugging
RewriteLogLevel 3
</VirtualHost>
Key points:
- The pattern is matched against the URL-path, not the filesystem path.
- Flags like
[R=301,L]trigger a 301 redirect and stop further processing. - To use
.htaccess, ensureAllowOverride FileInfo(orAll) is set for the target directory.
Verifying the Setup
- Check that mod_rewrite is loaded
Run on the server:apachectl -M | grep rewrite # Expected output: rewrite_module (shared) - Confirm the rule is active
Request the original URL and inspect the response:curl -I http://example.com/oldpage.html # Look for: # HTTP/1.1 301 Moved Permanently # Location: http://example.com/newpage.html - Review logs for rewrite activity
Apache’s access log will show the final URI. The error log will contain lines like:Rewrite: rule matched: ^/oldpage\.html$ /newpage.html - Test edge cases
Try a URL that should not match any rule (e.g.,/nonexistent.html) and verify a normal 404 response.
Recovery Options
If the rewrite rules break site functionality, you can:
- Revert the
VirtualHostor.htaccessfile to a known good state (use version control or a backup). - Disable the rewrite engine temporarily by setting
RewriteEngine Offand restarting Apache. - Check the syntax of the rewrite rule with
apachectl -tbefore restarting to catch errors early.
Limitations and Practical Checks
- Rewrite rules are evaluated in the order they appear; a misplaced rule can override subsequent ones.
- Complex conditions can degrade performance; test on a staging environment before production.
- Absolute URLs in rewrite targets can break when the site moves. Prefer relative paths.
- Always back up configuration files before making changes.
Conclusion
By following the steps above, you can confidently enable Apache’s mod_rewrite, write clear rules, verify their effect, and recover quickly if something goes wrong. The process is straightforward once the module is loaded and the rewrite engine is turned on.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.