Forum Moderators: phranque

Message Too Old, No Replies

Possible to comment-out more than one line at a time?

.htaccess, comment, ##

         

Martin Potter

1:37 am on Aug 15, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



In the .htaccess file, Is it possible to comment-out more than one line at a time with only a start and finish ## flag?

Something like :
## script line 1
script line 2
script line 3 ##


Thanks. About the .htaccess file, basically I know only what I have learned by example.

lucy24

1:45 am on Aug 15, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The easiest approach is to edit your htaccess in a text editor that allows block editing (multiple lines all at once) and then you can easily start a whole set of lines with a #, and later delete the # all at once. Just make sure you don't end up with trailing spaces if there are blank lines along the way.

Martin Potter

3:36 am on Aug 15, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks, Lucy24. I am using gedit which is nothing fancy so I will have to do it manually. And thanks for mentioning about trailing spaces -- I will try to avoid them. Cheers and thanks again!

whitespace

7:28 am on Aug 15, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Slightly related... to effectively "comment out" all the mod_rewrite (only) directives you can include a

RewriteEngine Off


directive at the end of the .htaccess file. Regardless of whether you have other RewriteEngine On directives earlier in the file.

keyplyr

8:31 am on Aug 15, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Or at the host level
Delete? Yes

phranque

9:10 am on Aug 15, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



https://httpd.apache.org/docs/2.2/configuring.html#syntax
Lines that begin with the hash character "#" are considered comments, and are ignored. Comments may not be included on the same line as a configuration directive.


the reference also says:
The backslash "\" may be used as the last character on a line to indicate that the directive continues onto the next line. There must be no other characters or white space between the backslash and the end of the line.

while i personally haven't tried a backslash to continue a .htaccess comment line, it might work...

graeme_p

10:18 am on Aug 15, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Gedit has a code comment plugin. That lets you highlight the lines you want to comment/uncomment as a block, right click, and choose "comment code" (or there is a key board shortcut for it).

Martin Potter

2:44 pm on Aug 15, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



Wow! Thank you, everyone, for all of those tips! Some I can use right now and some will help me later as I get more experience.
My original intent was to comment-out a block of code as an experiment and then be able to re-enable it, unchanged, without screwing up anything. (Probably after finding that I couldn't/shouldn/t do what I wanted to do. Ha!) Thanks to you all for this.
I have an old book by Ryan Bloom, "Apache Server 2.0 : the Complete Reference", which has been a big help to me for understanding some things, and no help at all with others. It seems our world is much more complicated now than it was with 2.0. Guess I should be using the on-line resources more for up to date guidance. Reading posts in this forum has been a huge help. It is a learning experience, thanks to all of you and your colleagues.

whitespace

5:01 pm on Aug 15, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



You could also do something like:


<IfDefine COMMENT_OUT>
script line 1
script line 2
script line 3
</IfDefine>


Providing you don't define COMMENT_OUT as a parameter on the httpd command line. You can also (temporarily) uncomment the block by preceding the parameter name with an exclamation mark (ie. not operator). For example:


<IfDefine !COMMENT_OUT>
:


IfDefine blocks can also be nested.

IfDefine blocks are processed very early, so you can even enclose wholly invalid syntax and it's simply ignored (like a real comment).

lucy24

5:21 pm on Aug 15, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Heh, whitespace, I was just thinking something similar, only I wasn't familiar with IfDefine so I thought maybe you could put the whole block inside something like
<IfModule some-made-up-name>
all-your-stuff-here
</IfModule>
But obviously since IfDefine exists, you'd do that. Good to see you back.

Martin Potter

2:02 am on Aug 16, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



whitespace and lucy24, well, you guys are way ahead of me. But I have made some more notes and maybe someday I can use that. :-) Thanks.

whitespace

10:30 am on Aug 16, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



while i personally haven't tried a backslash to continue a .htaccess comment line, it might work...


Interesting, this does seem to work. However, I think the intended use is so that you can simply comment out (ie. prefix with #) the first line of a multi-line directive and the whole directive is commented out, without having to line-comment each part of the single directive. Silly, but legitimate example:

#RewriteRule ^foo$\
/bar\
[R,L]


However, you can also do "bizarre" things like the following (not recommended):

#RewriteCond %{HTTP_HOST} example [NC]\
RewriteRule ^a$ /b [R,L]\
RewriteRule ^c$ /d [R,L]\
RewriteRule ^e$ /f [R,L]


All the above (separate) directives are "commented out". However, simply remove the # on the first line and you'll likely get a 500 error ("bad flag delimiters" in this example), since it tries to continue the directive (as if it was on the same line with no space). If you include a blank line in the middle of the "commented block" then the comment block stops/breaks at the line break.

The first commented line can be an ordinary comment, providing it ends in a backslash:

# All commented out\
RewriteRule ^a$ /b [R,L]\
RewriteRule ^c$ /d [R,L]\
RewriteRule ^e$ /f [R,L]


This will probably also confuse your code editors syntax highlighting.

(Thanks lucy24.)

lucy24

4:26 pm on Aug 16, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This will probably also confuse your code editors syntax highlighting.
Mine gets confused in the rare case of a mid-line # (probably only occurs when you're redirecting to a fragment). But the highlighting only applies to the end of the line.

My biggest worry about the final-backslash approach is that if you do use a text editor with block editing, you're all too likely to end up with a bunch of trailing black spaces, with catastrophic (i.e. 500-class) consequences. I could see myself selecting a block of lines, appending a backslash after the longest line--and then when I later delete the backslashes, all the editor-supplied spaces will still be there. So you'd have to put in and remove the backslashes manually.

I like the IfDefine approach. It makes me think of RewriteMaps, which can be used in htaccess although they can only be declared in config. So if you pick something fully nonsensical for the parameter, you're almost certainly good to go.

:: detour to test site ::

Yup, that works nicely. I made a redirect, confirmed that it works in isolation, and then tried
<IfDefine gibberish>
(rule is ignored) followed by
<IfDefine !gibberish>
(rule executes).