.htaccess

  • Don’t make assumptions about the file structure
    • Problem: Making modifications while assuming how other .htaccess modifications have been made can quickly lead to site-breaking results.
    • Example: cPanel’s hotlink protection code (WHM version 11.28.64) assumes that the .htaccess file ends in a newline. Due to this assumption, it adds new rules directly at the end of the file, potentially causing two rules to sit on the same line. When this happens, the site will break due to the syntax error in the file.
    • Solution: Never presume to know the state of a configuration file when modifying it. At a minimum, add an initial newline to protect against this issue. It is better to have unnecessary newlines than a syntax error that breaks the site. If necessary, first read the file to understand the format before making modifications.
  • Ensure that a script’s modifications are identifiable
    • Problem: Some scripts will add rules that cannot be easily identified and removed later.
    • Example: Take your pick of most hackish code that modifies .htaccess files.
    • Solution: Always wrap modifications in comments that uniquely identify the modification. This way it can be easily modified or removed if needed.