Apache .htaccess error: Invalid command ‘RewriteEngine’ – How to Fix

Updated: January 20, 2024 By: Guest Contributor Post a comment

The Error

If you’re encountering the ‘Invalid command ‘RewriteEngine” error in your Apache server’s .htaccess file, it usually indicates that Apache’s mod_rewrite module is not enabled or not installed. This module is necessary for URL rewriting, which is frequently used for website optimization, redirection, and ensuring the URL structure is user and SEO friendly.

Let’s Fix It

Solution 1: Enable mod_rewrite Module

The Apache mod_rewrite module provides a rule-based rewriting engine to rewrite requested URLs on the fly. Enabling this module generally resolves the ‘RewriteEngine’ error.

  1. Check if mod_rewrite module is enabled by running a2enmod rewrite command on your terminal.
  2. If it is not enabled, enable it by executing the command sudo a2enmod rewrite.
  3. Once enabled, restart the Apache server to apply changes. Use sudo service apache2 restart on Ubuntu or sudo systemctl restart httpd on CentOS.

TL;DR:

# Check if mod_rewrite is enabled
a2enmod rewrite

# Enable mod_rewrite module
sudo a2enmod rewrite

# Restart Apache server
sudo service apache2 restart

# or on CentOS
sudo systemctl restart httpd

Notes:

  • Pros: Enabling mod_rewrite will allow URL rewriting to function properly.
  • Cons: Requires server access and restart, which might cause a temporary downtime. Additional Apache configuration may be required for certain URL rewriting rules.

Solution 2: Verify .htaccess File Syntax

Improper syntax within the .htaccess file could result in the error. Ensure that the ‘RewriteEngine On’ directive exists and is correctly typed within the file.

  1. Open the .htaccess file in a text editor.
  2. Ensure the syntax is correct with no typos: the line should read RewriteEngine On, not ‘RewritEngine’, ‘Rewrite Engine’ etc.
  3. If the syntax is corrected, save the file and upload it back to your server (if you’re editing locally).

Example:

# Proper Syntax for .htaccess file
RewriteEngine On
# Following lines would contain rewrite rules

Notes: No need to restart Apache after modifying the .htaccess file.

  • Pros: Simple fix if it’s just a typo.
  • Cons: If the error persists after correction, the issue might be elsewhere.

Solution 3: Install mod_rewrite Module

In rare cases, the mod_rewrite module might not be installed at all. You would need to install it using Apache’s package manager.

  1. Install the mod_rewrite module.
  2. For Ubuntu, use sudo apt-get install libapache2-mod-rewrite.
  3. For CentOS, the module typically comes pre-installed with the httpd package.
  4. After installing or confirming installation, enable the module (see the first solution for enabling the module) and restart Apache.

Example:

# Install mod_rewrite module on Ubuntu
sudo apt-get install libapache2-mod-rewrite

# After installation, enable it
sudo a2enmod rewrite

# Restart the Apache server to apply changes
sudo service apache2 restart

Notes:

  • Pros: Ensures that the mod_rewrite module is present and can be enabled.
  • Cons: Requires server access and might not be necessary if mod_rewrite is already installed.