How to Enable mod_rewrite for Apache on Rocky Linux 8 (or Alma Linux 8)
If you’re trying to get pretty URLs working on your new Rocky or Alma server, you’ll hit the same roadblock: Apache ships without mod_rewrite turned on by default. The fix is quick – just a few commands and a tweak in the config – but it’s worth a word about what each step actually does.
Check if mod_rewrite Is Already Loaded
sudo httpd -M | grep rewrite
If nothing comes back, you’re staring at an empty module list. The command prints all compiled‑in modules, so you’ll know whether the file is missing or just not loaded. I’ve seen sites crash with 404s after a clean RHEL7=>Rocky8 migration exactly because this module never got activated.
Enable mod_rewrite With a Conf File
Open the base modules directory:
sudo nano /etc/httpd/conf.modules.d/00-base.conf
Add (or uncomment) the line:
LoadModule rewrite_module modules/mod_rewrite.so
Why bother with the file? Because Apache loads modules by reading these conf files at startup. Placing the LoadModule directive here keeps the module in sync with package updates – no need to remember it later.
Make .htaccess Work (Optional but Handy)
If your project relies on .htaccess, you must allow overrides:
sudo nano /etc/httpd/conf.d/allow-override.conf
Insert:
<Directory "/var/www/html">
AllowOverride All
</Directory>
without AllowOverride All, Apache will ignore any rewrite rules in your .htaccess files, and you’ll end up back at the 404 wall.
Reload Apache
sudo systemctl reload httpd
A reload is enough – it tells Apache to re‑parse its configuration without dropping existing connections. If you’re still not seeing rewrites work, double‑check that your .htaccess syntax is correct (a stray colon or missing slash can break everything).
Test It Out
Create a test rule:
# /var/www/html/.htaccess RewriteEngine On RewriteRule ^hello$ welcome.html [L]
Point your browser to http://your‑server/hello. If you land on welcome.html, you’ve got mod_rewrite happily rewriting URLs.
That’s the whole process in under five minutes. No unnecessary modules, no heavy installs, and a clear path back if something goes wrong.