Sling Academy
Home/DevOps/Apache .htaccess error: Invalid command ‘RewriteEngine’ – How to Fix

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

Last updated: January 20, 2024

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.

Next Article: Fixing Error: Apache shutdown unexpectedly

Previous Article: Apache URL rewriting error: CSS & JS files not loading (4 solutions)

Series: Apache Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide