Common causes
- An unescaped space in the pattern or target, so Apache reads the second half as the flags
- Flags not wrapped in square brackets, or a space inside them such as [R=301, L]
- Smart quotes, non-breaking spaces or other characters pasted from a website or word processor
- Windows line endings or a stray character at the end of the line
- A URL with spaces or %20 in the redirect target left unquoted
- Missing the target entirely, so the flags are read as the substitution and whatever follows becomes flags
How to fix it
- Find the exact line. The error log names the file; check each RewriteRule and RewriteCond, especially recently added redirects. Comment out new lines with # until the 500 stops.
- Check the three-part structure. Each rule must be RewriteRule Pattern Substitution [Flags], separated by single spaces. Make sure exactly three parts exist.
- Escape or quote spaces. Write spaces in patterns as \s or \ (backslash-space), or wrap the pattern or target in double quotes, for example RewriteRule "^old page$" /new-page [R=301,L].
- Fix the flag block. Use square brackets with comma-separated flags and no spaces: [R=301,L] or [NC,L]. Remove trailing characters after the closing bracket.
- Remove hidden characters. Retype quotes as plain ASCII, convert the file to UTF-8 without BOM and LF line endings, then upload again.
.htaccess (correct vs broken)
RewriteEngine On
# Broken: RewriteRule ^old page$ /new-page [R=301, L]
# Fixed:
RewriteRule "^old\ page$" /new-page [R=301,L]
# Redirect with a query condition
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^product\.php$ /product/%1? [R=301,L] How to stop it happening again
- Validate .htaccess changes before uploading
- Avoid spaces in URLs; use hyphens in slugs
- Edit config files in a code editor, never a word processor