About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
When a page returns 500 Internal Server Error and the Apache error log shows .htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration, the module that provides RewriteEngine, mod_rewrite, is not loaded into that Apache. Loading it fixes this error. If the log instead says RewriteEngine not allowed here, the module is loaded but AllowOverride does not permit that directive in .htaccess files.
What the error means
Apache reads .htaccess files on requests, and a directive it does not know, or is not allowed to use there, ends the request with a 500. The Apache documentation implies this in the description of the Nonfatal option of AllowOverride, which makes the server behave differently: "Instead of causing an Internal Server Error, disallowed or unrecognized directives will be ignored and a warning logged". Without that option, the Internal Server Error is what you get.
In the httpd 2.4.x source, the wording Invalid command '%s', perhaps misspelled or defined by a module not included in the server configuration is produced when no loaded module claims a directive name. A misspelled directive and a missing module look the same. The .htaccess code logs the file name, then a colon, then that message, and returns a 500. The text %s not allowed here is built when a directive is known but not permitted in the place where it was found.
Three settings interact:
- The module. The mod_rewrite page gives its module identifier as
rewrite_module.RewriteEnginehas the defaultoff. - The override class. The mod_rewrite page shows the override class for
RewriteEngineasFileInfo, so a.htaccessfile may use it only ifAllowOverridein the surrounding<Directory>section includesFileInfo(or isAll). - Options. The mod_rewrite page says that to use the rewrite engine in
.htaccessyou must setRewriteEngine Onand "at least one of the FollowSymLinks or SymLinksIfOwnerMatch Options must be enabled".
The default of AllowOverride is None, and the tutorial says: "This means .htaccess files are completely ignored unless you explicitly enable them for a directory." With None you do not get a 500 at all; the rules simply never apply.
Likely causes
| Cause | What you see in the error log | How to confirm |
|---|---|---|
mod_rewrite is not loaded |
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration |
apachectl -M does not list rewrite_module |
| The directive is misspelled | The same wording, naming the misspelled word | Compare the name in the log with the mod_rewrite documentation |
AllowOverride does not include FileInfo |
RewriteEngine not allowed here |
The <Directory> for the site's document root lists a different AllowOverride |
AllowOverride None |
No error and no 500, but rewrite rules never apply | The <Directory> section says AllowOverride None or sets nothing |
<IfModule> hides the missing module |
No 500, but the rules inside the section are ignored | The .htaccess wraps its rules in <IfModule mod_rewrite.c> |
No FollowSymLinks or SymLinksIfOwnerMatch |
Rules in .htaccess do not work although the module is loaded |
Options in effect for that directory |
Prove the cause before changing anything
All of these are read-only.
1. Read the error log line. The ErrorLog directive default on Unix is logs/error_log, relative to ServerRoot; your distribution may set another path. Find the line with the .htaccess path in it and the wording from the table.
2. List loaded modules.
apachectl -M
The httpd manual describes -M as "Dump a list of loaded Static and Shared Modules." Look for rewrite_module. The apachectl page says other arguments "are defined on the httpd manual page".
3. Find the site's configuration.
apachectl -S
-S shows "the settings as parsed from the config file (currently only shows the virtualhost settings)". It tells you which virtual host and configuration file serve the site, so you know where the <Directory> section is.
4. Read the <Directory> section for the document root. Note the AllowOverride and Options lines. Check whether the .htaccess file wraps its rules in <IfModule mod_rewrite.c>. The documentation says directives inside such a section are only processed if the test is true, and that "If test is false, everything between the start and end markers is ignored."
5. Check the configuration syntax.
apachectl configtest
The apachectl page says configtest "parses the configuration files and either reports Syntax Ok or detailed information about the particular syntax error" and is equivalent to apachectl -t. It does not prove your .htaccess is valid: the tutorial says "the .htaccess file is loaded every time a document is requested", so a bad directive in .htaccess shows up as a 500 on a request, not at configuration test time.
Fix
Load mod_rewrite
On Debian-family systems the a2enmod command enables modules. Its manual page says it works "by creating symlinks within /etc/apache2/mods-enabled" and gives a2enmod imagemap as the example for mod_imagemap. By the same naming, the module for mod_rewrite is rewrite. Record the current state first:
ls -l /etc/apache2/mods-enabled
apachectl configtest
Disruptive step: the next commands end with apachectl graceful, which restarts Apache gracefully. The apachectl page says graceful "differs from a normal restart in that currently open connections are not aborted" and that it "automatically checks the configuration files as in configtest before initiating the restart". Running requests finish, but the configuration change applies to every site on the server, not only yours. The back-out follows below. Then enable it and apply:
a2enmod rewrite
apachectl configtest
apachectl graceful
The Apache "Stopping and Restarting" page says of a graceful restart: "The parent re-reads its configuration files and re-opens its log files." The mod_so page describes loading modules "at start-up or restart time".
Disruptive step: if apachectl -M still does not list rewrite_module after the graceful restart, use apachectl restart instead. Because graceful is the form that does not abort open connections, a normal restart can abort requests in progress on every site on the server, so choose a quiet time. A restart changes no files, so there is nothing to restore; to undo the module change itself, use the a2dismod steps below.
Risk: other sites on the same server may rely on mod_rewrite. Disabling the module in the back-out below breaks their rules. Do not add -f (force), which also disables modules that depend on it.
To back out, disable the module again, test, and apply:
a2dismod rewrite
apachectl configtest
apachectl graceful
On other systems, add a LoadModule line. The mod_so page gives the form LoadModule module filename, with the example LoadModule status_module "modules/mod_status.so". The module identifier for rewriting is rewrite_module, and the file name and directory depend on your distribution, so copy the pattern of the other LoadModule lines in your configuration. Save the file before editing and test after:
cp -p /etc/httpd/conf/httpd.conf /root/httpd.conf.2026-11-20
apachectl configtest
Disruptive step: the graceful restart that follows your edit applies the change to live traffic on every site Apache serves. Edit the file, then test again and apply with apachectl configtest and apachectl graceful. To back out, copy the saved file back under the name you saved it, test, and apply again:
cp -p /root/httpd.conf.2026-11-20 /etc/httpd/conf/httpd.conf
apachectl configtest
apachectl graceful
The paths shown are examples; use the file the apachectl -S output points to.
Permit the override
If the log says not allowed here, set AllowOverride in the <Directory> section for the document root. Grant the narrowest class the rules need: FileInfo covers the rewrite directives. Make sure Options includes FollowSymLinks or SymLinksIfOwnerMatch, because the documentation says rewriting in .htaccess needs one of them. The documentation also says those options cannot be set in .htaccess unless AllowOverride permits it. First save the configuration file that apachectl -S pointed to (the path here is an example):
cp -p /etc/httpd/conf/httpd.conf /root/httpd.conf.allowoverride.2026-11-20
Disruptive step: changing AllowOverride in the edit below lets every .htaccess file under that directory use the new directive class, including ones you did not write. Run apachectl configtest after your edit and before graceful.
Edit the file, then test and apply:
apachectl configtest
apachectl graceful
To back out, copy the saved file back under the name you saved it, test, and apply again:
cp -p /root/httpd.conf.allowoverride.2026-11-20 /etc/httpd/conf/httpd.conf
apachectl configtest
apachectl graceful
Prefer the main configuration where you can
The tutorial gives two reasons to prefer the main configuration file over .htaccess: "performance and security". It adds that in .htaccess "regular expressions are recompiled on every request". If you control the server configuration, put the rewrite rules in the virtual host and keep AllowOverride None.
Fix a misspelling
If the log names a word that is not a real directive, the fix is in the .htaccess file. Save a copy outside the web root first, so the copy is not served:
cp -p /var/www/app/.htaccess /root/htaccess.2026-11-20
Risk: a corrected .htaccess file takes effect on the next request, with no test step.
Correct the spelling in the .htaccess file.
To back out, copy the saved file back under the name you saved it:
cp -p /root/htaccess.2026-11-20 /var/www/app/.htaccess
Example (illustrative): after a move to a new server, app.example.com on web01 returns 500 for every page. The error log shows /var/www/app/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration. apachectl -M does not list rewrite_module. The <Directory> for /var/www/app has AllowOverride AuthConfig, so .htaccess files are read but the class does not include FileInfo. The administrator lists mods-enabled, runs a2enmod rewrite, runs apachectl configtest, then apachectl graceful. Once apachectl -M lists rewrite_module, the log line changes to /var/www/app/.htaccess: RewriteEngine not allowed here and the pages still return 500. The administrator saves the configuration file, changes AllowOverride AuthConfig to AllowOverride AuthConfig FileInfo, runs apachectl configtest, then apachectl graceful, and the 500 stops.
Confirm the fix
apachectl -Mlistsrewrite_module.curl -sI https://app.example.com/returns a status other than 500.-Ifetches headers only.- A URL that depends on a rewrite rule returns the intended page or redirect.
- The error log shows no new
.htaccesslines for these requests. apachectl configteststill reports Syntax Ok (the apachectl page's wording), so the change did not break other sites.
What to watch afterwards
- Add a request to a rewritten URL to your monitoring, not only the home page. A missing module can leave the home page working.
- After you build or restore a server, compare
apachectl -Mwith the old server's list. - Check
AllowOverridewhenever a site changes hands or a control panel regenerates the configuration. - If you use the
Nonfatalform ofAllowOverride, remember it turns unknown directives into warnings, so rules can stop working without any 500. Prefer a loud failure while you are fixing a fault.
Read next
Related articles you may want next.
- TroubleshootingApache AH01630: client denied by server configuration (403 Forbidden)Apache 403 with AH01630 client denied by server configuration: find the Require rule that denies it and fix Directory scope and Order/Allow leftovers.Read article
- TroubleshootingERR_TOO_MANY_REDIRECTS after turning on HTTPS behind a proxy or CDNRedirect loop after enabling HTTPS behind a proxy or CDN? Trace it with curl -I -L, then fix X-Forwarded-Proto handling or the proxy encryption mode.Read article
- Troubleshootingnginx 403 Forbidden: "directory index of ... is forbidden" and "Permission denied" in the error lognginx 403 with 'directory index of ... is forbidden' or '(13: Permission denied)': read the error log line, then fix index, permissions or SELinux.Read article
Risks and limits
- These commands have not been run or lab-tested.
a2enmodanda2dismodare Debian tools. The a2enmod manual page namesimagemapas its example formod_imagemap, andrewritefollows the same pattern formod_rewrite.- The
LoadModulefile name and the configuration file paths depend on your distribution. mod_rewriterules can create redirect loops. For that fault see ERR_TOO_MANY_REDIRECTS after turning on HTTPS behind a proxy or CDN.- This applies to Apache 2.4. Other major versions are not covered.
Sources
- Apache mod_rewrite — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- Apache core features — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- Apache HTTP Server Tutorial: .htaccess files — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- apachectl — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- httpd — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- Apache mod_so — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- Stopping and Restarting Apache HTTP Server — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- Apache Log Files — Apache HTTP Server 2.4 documentation, no date shown, accessed 20 November 2026
- httpd 2.4.x source: server/config.c — Apache HTTP Server project, branch 2.4.x, no date shown, accessed 20 November 2026
- a2enmod(8) — Debian manpages, apache2 2.4.62, source last updated 2024-10-04, accessed 20 November 2026
This article was originally published on .