Ffile2fix
Sign in Get started

How to fix "RewriteRule: bad flag delimiters" in .htaccess

/home/user/public_html/.htaccess: RewriteRule: bad flag delimiters

Apache split a RewriteRule or RewriteCond line into parts and found something after the pattern and target that is not a valid [flags] block. This usually means an unescaped space, a missing bracket or a pasted special character has shifted the parts of the rule.

Also appears as: [core:alert] [pid 1234] [client 203.0.113.5:51234] /var/www/html/.htaccess: RewriteRule: bad flag delimiters · RewriteCond: bad flag delimiters · RewriteRule: unknown flag 'R=301 L' · AH00526: Syntax error on line 12 of /etc/apache2/sites-enabled/example.conf: RewriteRule: bad flag delimiters

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

  1. 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.
  2. Check the three-part structure. Each rule must be RewriteRule Pattern Substitution [Flags], separated by single spaces. Make sure exactly three parts exist.
  3. 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].
  4. 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.
  5. 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

Frequently asked questions

Why does a single bad line take down the whole site?

Apache parses the entire .htaccess for each request and treats a syntax error as fatal, returning 500 for everything under that folder.

Is [R=301, L] valid?

No. Spaces are not allowed inside the flag brackets; use [R=301,L].

How do I redirect a URL that contains %20?

RewriteRule matches the decoded path, so match a literal space with \s or an escaped space, or wrap the pattern in double quotes.