Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Apache refuses a RewriteRule with 'bad flag delimiters'

ErrorRewriteRule: bad flag delimiters

Short 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.

  • Applies to Apache httpd 2.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -t
  2. 2Match what you see, then jump to its fix

    1. 1The line ends in something like [R=301, L] or [NC, QSA]A space inside the flag listFix
    2. 2Flags written as R=301 L, [L][NC] or (L)Brackets missing or flags separated by something other than commasFix
    3. 3The 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

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 -t

Confirmed by httpd.apache.org

Did this fix it?

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/.htaccess

2Fix

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/test

Confirmed by httpd.apache.org

Did this fix it?

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 -t

Confirmed by httpd.apache.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.