Flags are the short codes in square brackets at the end of a rule, such as [R=301,L]. They decide what Apache does once an address matches: redirect the browser, quietly serve another file, block access, or ignore letter case. Without them a rule often works "almost" - and that "almost" is what takes longest to debug.
In short
Lstops processing,R=301redirects permanently,NCignores letter case.QSAkeeps the query string,QSDdrops it,BandNEcontrol how characters are encoded.Freturns 403,Greturns 410. Every example below was tested on Apache 2.4.68.
Where these examples come from
I tested every rule in this article on a local Apache 2.4.68 server in Docker, with curl and real response codes. How to set up such a server yourself in a few minutes is in A local Apache server in Docker for testing .htaccess. If RewriteRule is new to you, start with What is .htaccess?
The file always starts with:
RewriteEngine On
L - the last rule
L (last) says: if this rule matches, do not check the ones below. Without it, Apache carries on down the file, and a later rule may change what you just set up.
RewriteRule ^about/?$ /index.php?page=about [L]
One thing to watch: in .htaccess, after an internal rewrite Apache runs through the file again with the new address. L only ends the current pass. That is why "send everything to index.php" rules have a condition that lets index.php itself through (e.g. RewriteCond %{REQUEST_FILENAME} !-f), or you get a loop.
R - redirect
R sends the browser to a new address. The number is the response code:
RewriteRule ^old-page/?$ /new-page/ [R=301,L]
R=301- moved permanently. Browsers and Google remember the new address.R=302- temporarily. A plainRwith no number is a 302, which is easy to forget.
Test result: /old-page → 301 to /new-page/. Without R, the same rule would quietly serve the new content while the address bar kept the old address. When to use a 301 and when a 410 is covered in 301, 410 and noindex redirects.
When testing a 301, use a private window or curl. Browsers cache permanent redirects, and after you fix the rule they keep jumping to the old target.
NC - ignore letter case
RewriteRule ^contact/?$ /index.php?page=contact [NC,L]
Now /contact, /Contact and /CONTACT all end up in the same place. Test: /CONTACT → 200, and the script got page=contact.
QSA and QSD - what about the query string?
The query string is everything after the question mark, e.g. ?colour=red. By default, when a rule adds its own parameters, the original ones are lost.
QSA (query string append) merges them:
RewriteRule ^product/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
Test: /product/15?colour=red → the script receives id=15&colour=red. Without QSA, only id=15 would be left.
QSD (query string discard) does the opposite and drops the parameters. Useful in a redirect when the old address was covered in campaign tags:
RewriteRule ^old-shop/?$ /shop/ [QSD,R=301,L]
Test: /old-shop/?utm_source=fb → 301 to a clean /shop/.
B - encode what goes into a parameter
This one surprised me. A rule that passes a search phrase to a script:
RewriteRule ^search/(.+)$ /index.php?q=$1 [L]
The address /search/cats%20and%20dogs returns... a 403. Apache decodes %20 into a space and puts it into the parameter, and recent 2.4 releases refuse a rewritten address containing a space, for security reasons. The fix is the B flag, which encodes what $1 captured:
RewriteRule ^search/(.+)$ /index.php?q=$1 [B,L]
Test: 200, and the script receives q=cats and dogs. As a bonus, B stops an & in the phrase from splitting it into two parameters.
NE - do not encode the target
The opposite problem. A redirect to an anchor on a page:
RewriteRule ^offers/?$ /shop/#offers [R=302,L]
Result: a redirect to /shop/%23offers, a page that does not exist. Apache encoded the #. The NE flag (no escape) tells it not to:
RewriteRule ^offers/?$ /shop/#offers [NE,R=302,L]
Test: 302 to /shop/#offers. The same goes for targets with characters written as %C5%82. The redirects generator in Tools adds NE by itself for exactly this reason.
F and G - block or remove
Neither flag redirects; both return an error straight away. A dash in place of the target means "do not change anything".
# 403 Forbidden
RewriteRule ^private/ - [F]
# 410 Gone
RewriteRule ^sale-2019/?$ - [G]
Test: /private/file.txt → 403, /sale-2019 → 410. G is a good choice for a page removed for good with no sensible replacement - Google drops it from the results faster than with a plain 404. Both flags end processing themselves, so no L is needed.
Cheat sheet
L- stop, do not check further rules,R=301/R=302- permanent / temporary redirect,NC- ignore letter case,QSA- append the original query string,QSD- drop the query string,B- encode the captured part (spaces, &),NE- do not encode the target (#, %XX),F- 403,G- 410.
Combine flags with a comma and no spaces: [NC,QSD,R=301,L]. A space inside the brackets is a classic cause of a 500 error for the whole site. When something does not work, see Debugging .htaccess.