Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding comments to .htaccess

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
like image 629
user1032531 Avatar asked Feb 28 '14 22:02

user1032531


People also ask

Is a .htaccess file secure?

htaccess and . htpasswd files are protected from all external access. This is super important because you do not want anyone or anything to access these sensitive and powerful files.

What does htaccess command do?

. htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.


2 Answers

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.

like image 161
Jon Lin Avatar answered Sep 22 '22 13:09

Jon Lin


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.

like image 34
IMSoP Avatar answered Sep 23 '22 13:09

IMSoP