I have a very simple problem: I want to modify the .htaccess file. It seems I'm not the only person and I've seen the other questions and googled the issue. Nothing helps. Something keeps overwriting the file, sometimes causing it to throw an http 500 on the entire site.
What I already know or tried:
- I should place the custom htaccess rules outside the Wordpress block (marked by
# BEGIN/END wordpress
) - There is also another block from the caching plugin. My custom rules are outside this block too.
- Emptying the cache or en/disabling caching altogether does not trigger the problem.
- At first I put my custom rules between other
BEGIN
andEND
blocks that I gave a custom name, but I wasn't sure if Wordpress really looked for# BEGIN wordpress
or just for# BEGIN
, so I now use an entirely different name. - Some sources suggested chmodding the file (
-w
) so that it can't be changed anymore. I tried this already, it doesn't work, it just gets recreated. - There are no clear steps to reproducing the problem. Sometimes it'll last an entire evening without overwriting my code, another time all it takes is two minutes.
- When the file is overwritten, sometimes it retains part of my custom rules. Between one and two thirds usually, it seems pretty arbitrary. This regularly makes the entire site go HTTP 500 and I have to restore it over ssh (
git checkout .htaccess
). - I've grepped/read through some Wordpress' source code and it all seems to be coded as it should. I indeed see it checking for the BEGIN and END blocks (though the code is uncommented and ugly).
- I've also added
fclose(fopen('htaccess-changed','w'));
to the Wordpress function that writes the .htaccess file, but to no avail. The htaccess file got broken again but the htaccess-changed file is never created. - Even if I were to give up and just restore a working .htaccess version with every pageload, then still it wouldn't work because the damn thing throws http 500s from time to time. And having a cronjob run every minute is also... I mean come on, do I have to resort to such methods? Even if I did, it might be down randomly for a full minute. And nevermind it being bad practice on a shared webhost.
- My best guess at the moment is that a wordpress-cron triggers it, but it seems so random that I can't figure out which. It just happened three times in five minutes (finally thought it's reproducible, but nope it doesn't happen anymore now) and the most frequent cron is every quarter hour.
So I've been on this bug for a few days but I'm starting to simply run out of things to try. The site can't go live when it randomly breaks of course.
Any suggestions where to look for the code that overwrites the .htaccess file, and how to make it leave my modifications in place?
Installed plugins: Cherry plugin, Contact form 7, Register Plus Redux, W3 Total Cache and WooCommerce.
Update: The htaccess-changed file has been created, but in the wp-admin directory! I'm also saving the datetime and request-uri, and this shows that it can happen on any request. Mostly I see admin-ajax requests though, and those often happen at a very high rate, which would explain the htaccess file being completely broken (i.e. the requests interfere). Weird that nobody else experiences this though. But at least it's something in the admin panel... but what... Ideas?