A while back I wrote a post entitled “How To Redirect All Non-WWW Traffic To WWW“. I left out one important bit of info in that post for WordPress users.
If you update any info in the “General” options page, the redirection may no longer occur.
This happens because WordPress has a function called “flush_rules” which is called whenever you click Save in the General options page (not sure if it does it on other option pages). The function deletes the old rules then writes new ones in the .htaccess where your install is located. If you do what I did and made another htaccess there, it would simply be written over (and mucking around with permissions to prevent that is annoying and may not work, especially if your host runs PHP security extensions that run scripts under your user).
So, I looked around in the WordPress source and figured out how to prevent that from happening. The code is in the wp-includes/rewrite.php file. All I did was comment out the code inside the flush_rules() function by adding /* and */ before and after this code:
generate_page_uri_index();
delete_option('rewrite_rules');
$this->wp_rewrite_rules();
if ( function_exists('save_mod_rewrite_rules') )
save_mod_rewrite_rules();
Is now:
generate_page_uri_index();
delete_option('rewrite_rules');
$this->wp_rewrite_rules();
/*if ( function_exists('save_mod_rewrite_rules') )
save_mod_rewrite_rules();*/
I learned that you cannot comment out the first few lines, if you do you cannot create new pages, the last 2 lines are what save the data to .htaccess. Make sure you do not comment out the function declaration for flush_rules, or else you will get PHP errors.
This seemed to work perfectly for me, and now I don’t have to worry about WordPress’ “feature” becoming an annoyance.
Just a quick note: if you do this change and you want to change your blog’s URL, you should probably enable that code once more.
Please subscribe, or else I will cry. Do you really want to make a programmer cry?

August 7th, 2007 at 2:16 pm
[…] WordPress Users, Read This! - If you use WordPress you may want to take a look at my article on how to keep WordPress from writing over the .htaccess file. Because it auto-creates the file when updating your blog’s preferences, Thiefinder may not […]
August 27th, 2007 at 4:00 pm
[…] A while back I wrote a post called, How To Stop WordPress From Auto-Creating A Htaccess File. […]
September 1st, 2007 at 2:51 pm
[…] How To Stop WordPress From Auto-Creating A Htaccess File - This “feature” of WordPress is so absolutely annoying. I got sick of having to keep deleting the .htaccess that it auto-created (which broke my non-www to www redirector), so I disabled it and wrote up a tutorial explaining how I did it. […]