Apache refuses a RewriteRule with 'bad flag delimiters'
RewriteRule: bad flag delimitersShort answer
RewriteRule takes flags as one comma-separated list in square brackets at the end of the rule, with no spaces. Apache reports bad flag delimiters when that list is malformed: a space after a comma, a missing bracket, or an unquoted space in an earlier argument that pushes the flags into the wrong place.
Fix it fast
1Find out which cause you have
The error names the file and line. Open that exact line and compare it with the pattern RewriteRule pattern target [Flag1,Flag2].
apachectl -t2Match what you see, then jump to its fix
- 1
The line ends in something like [R=301, L] or [NC, QSA]A space inside the flag listFix - 2
Flags written as R=301 L, [L][NC] or (L)Brackets missing or flags separated by something other than commasFix - 3
The rule contains a space inside a path or a regular expression, such as (.*) with a literal spaceAn unquoted space in the pattern or target shifts the argumentsFix
- 1
What does the error look like?
RewriteRule: bad flag delimiters
Cause 1 of 3
A space inside the flag list
1Check
The docs describe flags as a comma-separated list in brackets, and every example has no spaces. Search your rules for a comma followed by a space.
grep -rn -E '\[[^]]*, [^]]*\]' /var/www/html/.htaccess /etc/apache2/sites-enabled/2Fix
Remove the spaces so the third argument is one token.
# wrong
RewriteRule ^old/(.*)$ /new/$1 [R=301, L]
# right
RewriteRule ^old/(.*)$ /new/$1 [R=301,L]3Verify
Run the syntax test, then reload.
apachectl -tConfirmed by httpd.apache.org
Cause 2 of 3
Brackets missing or flags separated by something other than commas
1Check
Flags must sit in one pair of square brackets as the last argument of the rule, with commas between them.
grep -rn 'RewriteRule' /var/www/html/.htaccess2Fix
Merge them into a single bracketed, comma-separated list.
# wrong
RewriteRule ^shop/(.*)$ /store/$1 [L][NC]
# right
RewriteRule ^shop/(.*)$ /store/$1 [NC,L]3Verify
Test the syntax and request a matching URL.
apachectl -t
curl -I http://example.com/shop/testConfirmed by httpd.apache.org
Cause 3 of 3
An unquoted space in the pattern or target shifts the arguments
1Check
Directive arguments are separated by whitespace, so a space inside an argument splits it into two. The flags then land in the wrong position.
grep -n 'RewriteRule' /var/www/html/.htaccess | grep -E '\S \S+ \S+ \S+'2Fix
Enclose the argument that contains the space in quotes, or match the space with the regex class \s instead of a literal space.
RewriteRule "^my files/(.*)$" "/files/$1" [L]
RewriteRule ^my\sfiles/(.*)$ /files/$1 [L]3Verify
Run the syntax test and reload.
apachectl -tConfirmed by httpd.apache.org
How do I stop it coming back?
- Copy flags from the official flag table and keep them in one bracket pair with commas only.
- Run apachectl -t after every .htaccess or vhost edit that touches rewrite rules.
- Quote the pattern and the target on every rule; the docs' own examples do.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
Laravel returns 500 Internal Server Error on Apache
/var/www/html/.htaccess: RewriteCond: bad flag delimiters
Apache and Laravel report different 500s.
5 causes, each with a fixServers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix