Diagnosing and Enabling mod_rewrite on Apache 2.4
If you are deploying WordPress or any PHP framework that relies on clean URLs, encountering a 404 error immediately after enabling a .htaccess rule usually points to one specific issue: mod_rewrite is not loaded or configured correctly. On CentOS 7 running Apache 2.4, this module often exists but remains disabled by default. The confusion typically stems from how Apache modules are managed versus how they are referenced in configuration files.
First, let's address the diagnostic step you attempted. Running a grep command against /etc/httpd/conf/httpd.conf to find LoadModule rewrite... is a logical starting point, but it often yields nothing because modern distributions, including CentOS 7, separate core modules from user-space configuration. The primary configuration file usually contains includes for other directories (like /etc/httpd/conf.d/) rather than listing every single module explicitly. Consequently, finding the LoadModule directive in the main .conf file is not a reliable indicator of whether the module is active.
To verify if mod_rewrite is actually loaded, you should use Apache's built-in status tool or inspect the module status directly. The most precise method involves checking the runtime configuration:
httpd -M | grep rewrite
If this command returns no output, the module is not active. You can also check the error log for specific warnings like AH00526: The mod_rewrite module is not loaded whenever Apache encounters a RewriteRule or RewriteCond directive it cannot process.
Enabling the Module Correctly
On CentOS 7 with Apache 2.4, modules are typically managed via the a2enmod utility (on Debian/Ubuntu) or by editing the /etc/httpd/conf.modules.d/ directory directly. Since CentOS uses RPM packages, the module is likely installed but disabled. You need to enable it by modifying the include files in the conf.d directory or adding the directive to the main config if no separate file exists.
The correct approach for Apache 2.4 on CentOS is often to add the following line to /etc/httpd/conf.modules.d/05_rewrite.conf (or create this file if it doesn't exist):
LoadModule rewrite_module modules/mod_rewrite.so
Alternatively, you can append this line directly to /etc/httpd/conf/httpd.conf. Once added, you must restart Apache for the changes to take effect:
systemctl reload httpd
After reloading, run httpd -M | grep rewrite again. If you see rewrite_module (shared), the module is active and ready to handle your .htaccess rules.
The Myth of "Loading All Modules"
Your source material raises a valid concern: does loading all available modules have negative consequences? In the context of Apache 2.4, enabling unused modules can slightly increase memory footprint and, in rare cases, introduce potential security vectors if those modules introduce new attack surfaces. However, for standard web applications like WordPress, mod_rewrite is essential.
The practice of "loading all modules" generally refers to leaving them installed but disabled (commented out) rather than removing them from the system. This is actually a recommended practice for CentOS/RHEL systems. It allows you to:
- Keep the package installed, avoiding dependency breakage if other packages rely on it.
- Enable specific modules only when needed, keeping the attack surface minimal.
- Avoid the overhead of compiling or reinstalling packages frequently.
Therefore, simply adding LoadModule rewrite_module ... is safe and does not degrade performance significantly. The real danger lies in leaving known-vulnerable modules enabled without patching, not in enabling a core module like mod_rewrite.
Troubleshooting Persistent Issues
Even after enabling the module, WordPress pretty URLs might still fail. This usually indicates a conflict between your virtual host configuration and local .htaccess files. In Apache 2.4, if you have set up your VirtualHost to use <Directory> blocks with AllowOverride None, any .htaccess rules will be ignored regardless of whether mod_rewrite is loaded.
Ensure your VirtualHost configuration in /etc/httpd/conf.d/ or the site-specific config includes:
<Directory /var/www/html>
AllowOverride All
</Directory>
Without AllowOverride All, Apache will not read .htaccess files, effectively disabling pretty URLs even if mod_rewrite is perfectly configured. This is a common pitfall that leads developers to believe the module isn't working when the issue is actually permission-based configuration within the virtual host definition.
