How to Enable and Configure mod_rewrite in Apache on Ubuntu: A Complete Guide for Developers


2 views

The mod_rewrite module is an Apache extension that provides a rule-based rewriting engine to rewrite requested URLs on the fly. It's commonly used for:

  • URL beautification and SEO-friendly links
  • Redirecting old URLs to new locations
  • Preventing hotlinking of images
  • Implementing custom routing in web applications

Before enabling, verify if mod_rewrite is already loaded:

apache2ctl -M | grep rewrite
# or
apachectl -M | grep rewrite

If you see "rewrite_module" in the output, it's already enabled.

For Ubuntu systems (including 10.04 and later versions), the correct method is:

sudo a2enmod rewrite
sudo systemctl restart apache2
# For older systems without systemctl:
sudo /etc/init.d/apache2 restart

After enabling the module, you need to allow .htaccess overrides in your virtual host configuration. Edit your site's configuration file:

sudo nano /etc/apache2/sites-available/000-default.conf

Add or modify the Directory section:

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Here are some common .htaccess rewrite rules:

# Example 1: Remove .php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Example 2: Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Example 3: Clean URL routing
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [NC,L]
  • 403 Forbidden errors: Check directory permissions and AllowOverride settings
  • 500 Internal Server Error: Usually indicates syntax errors in .htaccess
  • Rules not working: Verify that mod_rewrite is actually loaded and that the .htaccess file is being read

To check if your .htaccess is being processed, add a deliberate syntax error and see if you get a 500 error.

While mod_rewrite is powerful, complex rule sets can impact performance. Tips:

  • Place more specific rules before general ones
  • Use the [L] flag to stop processing after a match
  • Consider using FallbackResource for simple front controllers
  • Cache frequently used rewrite rules when possible

For some use cases, consider these alternatives:

# Simple redirects:
Redirect 301 /old-path /new-path

# Alias for static path mapping:
Alias /static /var/www/special-static-files

The Apache mod_rewrite module is essential for URL manipulation and redirection. Unlike simply copying files between directories, proper activation requires both module loading and configuration file adjustments.

# First, enable the module using a2enmod
sudo a2enmod rewrite

# Then restart Apache to apply changes
sudo service apache2 restart

Check if mod_rewrite is active with:

apache2ctl -M | grep rewrite

You should see rewrite_module in the output if successful.

Edit your virtual host configuration (typically in /etc/apache2/sites-available/000-default.conf):

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Create or modify .htaccess in your web root:

# Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Clean URLs example
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [NC,L]
  • Ensure AllowOverride All is set in your vhost config
  • Check Apache error logs (/var/log/apache2/error.log)
  • Verify file permissions on .htaccess

For high-traffic sites:

# Disable directory listing
Options -Indexes

# Disable .htaccess parsing when possible
AllowOverride None