Adding comments to .htaccess

Apache.HtaccessComments

Apache Problem Overview


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

Apache Solutions


Solution 1 - Apache

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.

Solution 2 - Apache

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1032531View Question on Stackoverflow
Solution 1 - ApacheJon LinView Answer on Stackoverflow
Solution 2 - ApacheIMSoPView Answer on Stackoverflow