Ffile2fix
Sign in Get started

How to fix a 500 error after editing .htaccess

/home/user/public_html/.htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration

When a site shows 500 Internal Server Error right after an .htaccess edit, Apache could not parse the file. One bad line, a missing module or a directive not allowed in .htaccess breaks every request in that folder. The error log names the exact problem.

Also appears as: /home/user/public_html/.htaccess: <IfModule takes one argument, Container for directives based on existance of specified modules · /home/user/public_html/.htaccess: RewriteRule: bad flag delimiters · /home/user/public_html/.htaccess: php_value not allowed here · /home/user/public_html/.htaccess: Expected </IfModule> but saw end of file

Common causes

  • A directive from a module that is not loaded (Header needs mod_headers, Expires needs mod_expires)
  • php_value or php_flag used when PHP runs as PHP-FPM or CGI, not mod_php
  • Unclosed <IfModule> or <Files> blocks
  • Bad RewriteRule flags, such as spaces inside [L, R=301]
  • A directive that is not allowed in .htaccess under the current AllowOverride setting
  • Smart quotes or Windows encoding introduced by a word processor

How to fix it

  1. Restore the site first. Rename .htaccess to .htaccess.broken or re-upload your last working copy. The site should load again, which confirms the edit caused the 500.
  2. Read the exact error. Check the Apache error log or cPanel > Errors. It names the file, and usually the directive that failed, such as Invalid command 'Header'.
  3. Remove php_value lines on PHP-FPM. If the log says php_value or php_flag is invalid, PHP is not running as an Apache module. Move those settings into .user.ini or the host's PHP settings.
  4. Wrap module directives in IfModule. Put Header lines inside <IfModule mod_headers.c> and Expires lines inside <IfModule mod_expires.c>, or enable the module with a2enmod headers expires.
  5. Fix flag and block syntax. Write flags with no spaces, for example [L,R=301,NC]. Make sure each <IfModule> has a matching </IfModule>.
  6. Re-add changes one at a time. Add the new lines back in small groups and reload after each. When the 500 returns, the last group contains the bad line.

Safe pattern: module directives guarded by IfModule

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
</IfModule>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/webp "access plus 1 month"
</IfModule>

How to stop it happening again

  • Keep a backup copy before every .htaccess edit
  • Edit with a plain text editor, never a word processor
  • Validate the file before uploading
  • Use .user.ini for PHP settings on PHP-FPM hosts

Frequently asked questions

Why does one wrong line break the whole site?

Apache reads .htaccess on every request in that folder and its subfolders. If it cannot parse the file, it refuses to serve any of those requests.

Where can I see the .htaccess error?

In the Apache error log, often /var/log/apache2/error.log, or in cPanel under Metrics > Errors. The browser only shows a generic 500.

Can I put php_value in .htaccess?

Only when PHP runs as mod_php. Most modern hosts use PHP-FPM or LSAPI, where you set PHP values in .user.ini or php.ini instead.