Post Date: April 1, 2017
Last Updated: September 28, 2026
Introduction
Apache’s mod_rewrite module is commonly used to create URL redirects and rewrite rules. It is also required for .htaccess rewrite rules to work correctly on an Apache web server. This guide explains how to enable the mod_rewrite module on Apache 2.4 and configure the required AllowOverride setting.
Prerequisites
Before enabling the mod_rewrite module, ensure that:
- Apache 2.4 is installed on the server.
- You have root or sudo access to the server.
- The website’s document root is identified.
- You have permission to modify the Apache virtual host configuration.
- It is recommended to take a backup of the Apache configuration before making changes.
Implementation:
Log in to the server as root.
Execute the following command to enable the mod_rewrite module.
a2enmod rewrite
Restart the Apache service to take effect.
service apache2 restart
If you followed the above post, there will be a default configuration file, 000-default.conf. Open the file and add the following content.
vi /etc/apache2/sites-enabled/000-default.conf
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
If the document root is different from/var/www/html then replace it with the appropriate directory.
Restart the Apache service for the changes to take effect.
service apache2 restart
Verification:
1. Execute the command below and look for the rewrite module in the output.
apachectl -M|grep rewrite
2. Create a .htaccess file in the directory and add a simple rewrite rule.
vi /var/www/html/.htaccess
RewriteEngine on
Access the domain or IP address (http://IPAddress) in the browser. If you see a 500 Internal Server Error, then there is a problem with the rewrite module.
Look for any errors while accessing the page in the browser using the command. If you see any error related to Rewrite, then there is a problem with the rewrite module.
tail -fn0 /var/log/apache2/error_log
In case of any issue, please comment here.
Conclusion
Enabling mod_rewrite allows Apache to process URL rewrite rules and .htaccess configurations. By enabling the module, setting AllowOverride All for the appropriate document root, and restarting Apache, rewrite rules can be used successfully. The verification steps can be used to confirm that the module is enabled and identify any configuration-related issues.
FAQs
1. Is mod_rewrite available by default in Apache 2.4?
The mod_rewrite module is included with Apache 2.4, but it may not be enabled by default on some Linux distributions. You can enable it using a2enmod rewrite.
2. Is AllowOverride All safe to use for every Apache directory?
It is generally better to enable only the directives required by the application. AllowOverride All permits .htaccess to override a broad range of Apache settings, so it should be applied only where required.
3. What should I do if .htaccess rewrite rules are not working after enabling mod_rewrite?
Verify that mod_rewrite is enabled, AllowOverride permits .htaccess rules for the document root, and the .htaccess syntax is correct. You can also check the Apache error log for configuration or rewrite-related errors.
Related Articles
How to enable mod_rewrite for Apache in CentOS
Talk to our experts
Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.