向.htaccess 添加注释

为什么会这样:

RewriteRule (.+)/$ $1

还有这个工作:

RewriteRule (.+)/$ $1 [L] #bla bla bla

但这不管用:

RewriteRule (.+)/$ $1 #bla bla bla
65933 次浏览

Comments in .htaccess must be on their own line, not appended to other statements.

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.

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.