Why does this work:
RewriteRule (.+)/$ $1
and this work:
RewriteRule (.+)/$ $1 [L] #bla bla bla
but this doesn't work:
RewriteRule (.+)/$ $1 #bla bla bla
Join Stack Overflow to learn, share knowledge, and build your career.
The last rule doesn't work because the comments aren't really comments. Comments in htaccess must begin with a #
(must be at the start of a line), and not arbitrarily anywhere.
In the second case, the #bla bla bla
is interpreted as a 4th parameter of the RewriteRule
directive, which is simply ignored.
In the last case, the #bla bla bla
is interpreted as a 3rd parameter, which in the RewriteRule
's case is where the flags go, and #bla bla bla
isn't any flags that mod_rewrite understands so you get an error.
#
must be at the "start" of the line, it can be preceded by any amount of whitespace. (Excess) Whitespace in .htaccess is ignored, and can be "sprinkled" anywhere, including at the beginning of lines. In other words, #
must be the first non-whitespace character on a line. I erroneously used to think #
had to be character 1, in the left-most column!
– SherylHohman
Feb 10 '19 at 5:22
Apache's config file format (of which .htaccess
files are one example) doesn't technically support inline comments, only full-line comments (i.e. a line beginning with a #
).
Lines that begin with the hash character "#" are considered comments, and are ignored. Comments may not be included on a line after a configuration directive. -- Official Apache 2.4 manual
Confusingly, though, each module gets to parse the input for its directives however it likes - so mod_rewrite
decides what to do with any line beginning with RewriteRule
I don't know for sure but my guess is that mod_rewrite
is ignoring everything after the [flags]
, and the #
isn't actually necessary at all.
Best bet, though, is to always keep comments to their own line, since that will work whatever the directive you're commenting.