Adding Comments to a .htaccess File

Introduction

Using the .htaccess file in Apache web server is a bit of a pain, although its hard to steer away from it. To help organise your htaccess file, like any other code, it is useful to add comments.

How To Add Comments

Simply enough, it is a case of using the # (hash) symbol.

So for example for Daft Logic, we may have a file such as ...


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.daftlogic.com$ [NC]
RewriteRule ^(.*)$ https://www.daftlogic.com/$1 [R=301]
RewriteRule ^(['_A-Za-z0-9-]+).htm$ /?showpage=$1 [L]

Could be re-written as...


RewriteEngine On

#Redirect all non-www traffic to the www version of site (prevents possible duplicate content)
RewriteCond %{HTTP_HOST} !^www.daftlogic.com$ [NC]
RewriteRule ^(.*)$ https://www.daftlogic.com/$1 [R=301]

#Rewrite all pages ending in .htm to the index page, with the preceding string as the showpage GET variable
RewriteRule ^(['_A-Za-z0-9-]+).htm$ /?showpage=$1 [L]

As far as Apache sees there is no difference as it ignores the lines starting with #, but it can be much easier to see what's going on. This is helpful because the nature of the htaccess file means you do not use it day in day out, but are more likely to forget about it for a few months and when you look at it again it is easy to forget why it is what it is.

Comments For This Page

Hi Bruce, Unfortunately not.

It is fussy about the hash only appearing as the first character on a line. If the hash appears after another command it will likely cause an internal server error.
By Daft Logic on 10th April 2017
Must a comment always start on a new line? Could I for instance put a comment at the end of a line eg
RewriteCond %{HTTP_HOST} !^www.daftlogic.com$ [NC] #Redirect all non-www traffic to the www version ?
By Bruce on 10th April 2017
Perfect Answer, Thanks!
On 8th June 2016
Thanks!
By Alex on 20th April 2016
Thanks, Daft Logic.
By Luis on 27th March 2016
ASD
On 18th January 2016
Hi, this requires you use multiple # symbols per line, so no, there is no multi line method.
By Daft Logic on 18th March 2015
Any way to make a block comment e.g., for multiple lines?
On 18th March 2015
Thanks !
On 8th December 2014
allo
On 30th November 2013
Thanks, comes in v.useful
By Jamie on 18th December 2012
ThanK You
On 8th August 2012
awesome. thanks.
On 4th March 2011
Just what I needed to know! Cheers
On 30th June 2009
thanks
On 23rd February 2008

Add your own comment below and let others know what you think: