Ffile2fix
Sign in Get started

How to fix "Invalid command 'RewriteEngine'" in Apache

/var/www/html/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Apache read a RewriteEngine line but the module that provides that directive, mod_rewrite, is not loaded, so it does not recognise the command. In .htaccess this produces a 500 Internal Server Error; in the main config it stops Apache from starting.

Also appears as: Invalid command 'RewriteRule', perhaps misspelled or defined by a module not included in the server configuration · AH00526: Syntax error on line 2 of /etc/apache2/sites-enabled/000-default.conf: Invalid command 'RewriteEngine' · Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration · Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration

Common causes

  • mod_rewrite is installed but not enabled, which is the default on fresh Debian/Ubuntu Apache installs
  • On RHEL/Alma/Rocky the LoadModule rewrite_module line is commented out in conf.modules.d
  • The same issue with other modules: Header needs mod_headers, ExpiresActive needs mod_expires
  • php_value or php_flag in .htaccess while PHP runs as PHP-FPM/CGI instead of mod_php
  • A typo or invisible character (BOM, smart quotes) at the start of the line
  • The server is not Apache (for example Nginx proxying to a minimal Apache) and lacks the module

How to fix it

  1. Confirm which directive failed. Read the Apache error log (/var/log/apache2/error.log or /var/log/httpd/error_log). The message names the exact directive and the file and line.
  2. Enable mod_rewrite on Debian/Ubuntu. Run sudo a2enmod rewrite, then sudo apachectl configtest and sudo systemctl restart apache2.
  3. Enable it on RHEL-based systems. Check that LoadModule rewrite_module modules/mod_rewrite.so is present and uncommented in /etc/httpd/conf.modules.d/00-base.conf, then restart httpd.
  4. Enable other missing modules the same way. For Header run a2enmod headers; for ExpiresActive run a2enmod expires. Verify with apachectl -M | grep -E 'rewrite|headers|expires'.
  5. Replace php_value under PHP-FPM. When PHP runs via FPM, php_value is invalid in .htaccess. Move those settings to .user.ini in the site root or to the FPM pool config.
  6. Ask your host on shared hosting. Shared cPanel hosts nearly always load mod_rewrite. If you see this there, check for typos and hidden characters, or contact support.

Shell + .htaccess

# Debian / Ubuntu
sudo a2enmod rewrite headers
sudo apachectl configtest && sudo systemctl restart apache2
apachectl -M | grep -E 'rewrite|headers'

# .htaccess: guard rules so a missing module does not cause a 500
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>

How to stop it happening again

  • Include a2enmod rewrite headers in your server provisioning script
  • Wrap optional rules in <IfModule> blocks in distributable .htaccess files
  • Run apachectl configtest before every restart

Frequently asked questions

Should I always wrap rewrite rules in <IfModule mod_rewrite.c>?

It prevents a 500 error when the module is missing, but the rules silently stop working, which can leave pretty URLs returning 404. Enabling the module is the real fix.

Why does php_value give the same "Invalid command" error?

php_value is provided by mod_php. Hosts using PHP-FPM, LiteSpeed's LSAPI or CGI do not load it, so use .user.ini or the host's PHP settings page instead.

Do I need AllowOverride as well?

Yes, for .htaccess rules to be read at all AllowOverride must permit them (FileInfo for rewrites). A missing AllowOverride causes rules to be ignored rather than this error.