I'm using a PHP e-commerce script which is full of comments which are visible to the user if they were to view the source. It doesn't look professional. The comments are everywhere, and it would not be practical to remove them manually.

Is there a way to disable the comments from being displayed via Apache?

The comments I am refering to are the ones like this:

<!-- Start of some script -->

<!-- End of some script -->

  • You should probably be looking at programming solutions for this. Either that or deployment tools. – Reaces Jan 29 '15 at 9:31
  • 2
    Duncan below is right that very few people look at the source; you can look at things like page speed or you can use a proxy that does it for you. But if your only motivation is because "somebody might look at the source" your priorities are in the wrong place. Note that requests for product recomendations are off topic, so don't expect 10 different answers with 10 different vendor-solutions. – AD7six Jan 29 '15 at 9:39
  • @Reaces don't know what to say - fire your devs? Why are end-users seeing html comments describing sensitive information? Script files definitely should have comments; PHP files should not be outputting html comments with that kind of info (ever). Also note I gave 2 suggestions as to how to remove comments :P. – AD7six Jan 29 '15 at 9:44

You shouldn't really worry about your source code not "looking professional" - very few people view the source.

I don't think there's a way to do this via Apache, and if there is a way it's definitely not a good idea (performance-wise and generally).

If you want to batch remove the comments from your source files, I would recommend using sed with the pattern s/<!--.*-->//g:

cd /path/to/website
find . -type f -print0 | xargs -0 sed -i 's/<!--.*-->//g'
  • "I would recommend using sed with the pattern s/<!--.*-->//g." - How exactly do you expect that to work? – AD7six Jan 29 '15 at 9:29
  • @AD7six Updated. – user267609 Jan 29 '15 at 9:32
  • @AD7six there is a copy-and-paste command to achieve OP's goal, what more do you want? – user267609 Jan 29 '15 at 9:34
  • 1
    @duncan if you're going to answer a question like this (imo a bad idea) - read the question. "it would not be practical to remove them manually" - So the OP doesn't want to do that. In any event applying your sed command to script files can easily break things e.g. of ONE example: <!-- [if IE ]>. – AD7six Jan 29 '15 at 9:41
  • 1
    Thanks. I will have a look and consider leaving it as it is. Just some of the comments are there to remove code to later review, it just looks messy. – user2924019 Jan 29 '15 at 10:34

I would use a minifying output filter. Something like:

You can also think to cache parts of the output of the minify if the minify+tempalte process is slow. But this could mean a redesign of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.