What is mod_rewrite?
Apache is a popular web server program. A module is an add-on that gives it a new skill. mod_rewrite changes web addresses on the fly. It can turn example.com/index.php?page=about into example.com/about. WordPress uses it for its pretty links.
On shared hosting with cPanel, mod_rewrite is normally on already. You just use a file called .htaccess. The steps below are for your own VPS.
Warning
A mistake in Apache settings can take your site offline. Back up the config files before you edit them.
Steps on Ubuntu or Debian
- Connect with SSH.
- Turn on the module:
sudo a2enmod rewrite
This switches on mod_rewrite.
- Open your site's config file. The path may differ, for example:
sudo nano /etc/apache2/sites-available/000-default.conf
- Inside the
<VirtualHost>block, add:
<Directory /var/www/html>
AllowOverride All
</Directory>
This lets .htaccess files in that folder change how Apache behaves. Change the path to your web folder.
- Test the config and restart Apache:
sudo apache2ctl configtest
sudo systemctl restart apache2
The first line checks for typing mistakes. The second restarts Apache.
Steps on CentOS, AlmaLinux or Rocky
The module is usually included. Check /etc/httpd/conf/httpd.conf for the line LoadModule rewrite_module modules/mod_rewrite.so. Set AllowOverride All for your web folder. Then run sudo systemctl restart httpd.
Test it
- In your web folder, create or open
.htaccess. - Add this simple rule:
RewriteEngine On
RewriteRule ^hello$ index.html [L]
This makes /hello show your index.html page.
- Visit
yourdomain.com/hello. If it works, mod_rewrite is active.
/var/log/apache2/error.log.Quick recap
- mod_rewrite makes clean URLs and redirects on Apache.
- Enable it with
a2enmod rewriteon Ubuntu or Debian. - Set
AllowOverride Allfor your web folder. - Test the config, restart Apache and try a rule.