Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Set Up mod_rewrite on Apache

mod_rewrite lets you create clean web addresses and redirects. This guide shows how to turn it on and test it. It needs root access on a VPS.

VPS and Cloud Servers1 min read8 steps

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

  1. Connect with SSH.
  2. Turn on the module:
sudo a2enmod rewrite

This switches on mod_rewrite.

  1. Open your site's config file. The path may differ, for example:
sudo nano /etc/apache2/sites-available/000-default.conf
  1. 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.

  1. 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

  1. In your web folder, create or open .htaccess.
  2. Add this simple rule:
RewriteEngine On
RewriteRule ^hello$ index.html [L]

This makes /hello show your index.html page.

  1. Visit yourdomain.com/hello. If it works, mod_rewrite is active.
Tip: If you get a 500 error, read the Apache error log. It is often at /var/log/apache2/error.log.

Quick recap

  • mod_rewrite makes clean URLs and redirects on Apache.
  • Enable it with a2enmod rewrite on Ubuntu or Debian.
  • Set AllowOverride All for your web folder.
  • Test the config, restart Apache and try a rule.