Tag Archives: RewriteLog

Apache, .htaccess, mod_rewrite, git and CRLF

Today I came across a frustrating and ultimately difficult to debug situation brought about by a cocktail of circumstances. Working on a site the rewrite rules suddenly stopped working when I made a small change to the .htaccess file. Nothing unusual here, back up the change and carry on. Except backing up the change made no difference. Ultimately the site still wouldn’t work.

Two fruitless hours later checking and rechecking syntax, conf directives and even the changing of DOCUMENT_ROOT just in case and still the issue wouldn’t resolve. The only thing left to do was to start again.

Delete the files, check the repos back out of git and see what happened. Magically it started working again, obviously something I had done had broken the site but what could it be. I retraced my steps and got once again to the .htaccess file and again the site broke. Instead of backing up the change I simply rechecked it out of git, which again fixed the issue. I then opened the file, made no changes and saved it, and this broke the site.

So basically it worked fine from the checkout of git, but not once my text editor had touched it. This of course screamed line endings at me, and indeed this was the culprit. CRLF in the repos, LF when it hit my editor.

What was strange here was that it worked when it was from git, but not when my editor had touched it. Even stranger was that Apache couldn’t tell me if anything was wrong no matter what steps I took to debug. Nothing in the logs the .htaccess file was simply ignored silently. Not very helpful.

The answer lay on github, where setting a global config setting in git would handle this situation gracefully but tracking down the issue was very hard.

git config --global core.autocrlf input

Fixing it was a simple matter of using dos2unix, a utility I had experience with due to similar issues with Perl scripts back in my early days as a programmer.

As ever Google turned out to be my friend, but only after I had diagnosed the issue, searching for issues with .htaccess files had a far too high signal to noise ratio.

Another one to chalk up to experience.