Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comment out whole sections of a .htaccess file?

Is it possible to comment out one or more sections of an .htaccess file, like you would using /* and */ in various programming languages?

like image 681
manu3d Avatar asked Jun 02 '16 17:06

manu3d


People also ask

How do you comment a large section of code?

The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .


2 Answers

Strictly speaking, .htaccess files only allow single-line comments: an hash character (#) at the beginning of a line lets the parser know that line should be ignored, i.e.:

# this is a comment in an .htaccess file and many other scripting languages

However, from a practical perspective it is possible to wrap any number of contiguous lines in an IF block (available from Apache 2.4). This effectively disables the lines within the block. For example:

<IF "false">
...disabled directives...
</IF>

That been said, a multi-line comment in many programming languages would allow more or less any content within it, i.e. plain english rather than viable code. Conversely, the content of an IF block as mentioned above must be composed of proper .htaccess directives and regular single-line comments - an http 500 error will be generated otherwise.

like image 191
manu3d Avatar answered Sep 23 '22 00:09

manu3d


Expanding and giving an example of Manu3D answer, you can encapsulate the comment content into a FilesMatch directive and use an improbable filename for the match.

<FilesMatch "index-sbrubles123land9897Brazil\.(php?)$">
    #commented content here
</FilesMatch>

But, I would avoid to use many comments and specially this "faux multiline comment" technique in a production environment. I think the less time Apache spend parsing .htaccess files, the better (less prone to errors).

And don't forget that you can't use text or any invalid apache syntax inside this false comment, anyway. It would be helpful to temporarily or conditionally disabling other directives, but not to comment text.

like image 29
Celso Bessa Avatar answered Sep 24 '22 00:09

Celso Bessa