Home Services Contact Info

Stop Wordpress Overwriting Custom .htaccess Rules

Posted in: Blogs, WebDev by Richard Hearne on June 11, 2007
Internet Marketing Ireland

For as long As I care to remember I’ve been having issues with my Wordpress .htaccess file.

.htaccess file is a small Apache file that lets you do all sorts of funky things with requests made to your server. It’s also one of SEO’s best tools. I have a lot of custom 301 redirects set up, including a redirect which makes my site available only via the www subdomain.

Wordpress Permalinks

Well Wordpress has a habit of rewriting the .htaccess file to allow some of the SEO-friendly URLs you regularly see (also known as ‘permalinks’). And each time it does so I lose my rules. It’s a royal pain in the arse and when this happened just the other day I thought I’d take the time to fix this for once and for all. I had to dig through the Wordpress Codex to see what was causing all the trouble. save_mod_rewrite_rules() is the culprit. That little function, and my own ignorance of how Wordpress processes the .htaccess file.

The solution

As with most solutions it’s really very simple. As with most simple solutions it’s only simple if you know about it. So here it is:

Wordpress .htaccess file looks like this:

# BEGIN wordpress
<ifmodule mod_rewrite.c>
rewriteEngine On
rewriteBase /
rewriteCond %{REQUEST_FILENAME}!-f
rewriteCond %{REQUEST_FILENAME}!-d
rewriteRule . /index.php [L]
</ifmodule>
# END wordpress

Now here’s the really important bit:

Never place your own rules within the ‘wordpress’ block

The Wordpress block is the bit that starts with # BEGIN wordpress and ends with # END wordpress. My mistake was to place my rules within this block (after the rewirteEngine On line). This seemed the sensible thing to do – after all rewrite rules must come after rewirteEngine On, and my understanding was not to repeat this command.

How Wordpress rewrites .htaccess files

When Wordpress rewrites the .htaccess file it does so by first checking that the file is writeable, then exploding the file using the # BEGIN and # END strings. Anything outside of those markers should be left intact after WP rewrites the file.

In my case I had to add a new block with a second rewirteEngine On so that Apache wouldn’t break (although I don’t think this is strictly the correct way to write the file). Here’s what my new revised .htaccess file looks like:

<ifmodule mod_rewrite.c>
RewriteEngine On
[... ]Funky custom rules go here[ ...]
</ifmodule>
# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress

Perhaps the Wordpress folk could add an additional comment into the .htaccess file that explains this better?

Well there you have it – how to stop Wordpress overwriting your custom .htaccess file rules.

You should subscribe to the RSS Feed here for updates.
Or subscribe to Email Updates now:

20 Comments »

  1. Thanks Richard, I’m off to see if I’ve got anything within the wordpress block.

    Comment by JLH — June 11, 2007 @ 10:11 pm

  2. hiya,
    I do mine the same way, but one question the “” that you have ? Is this just to stop Apache crashing incase great sysadmin forgets to reinstall mod_rewrite ?
    thanks,
    paul

    Comment by paul — June 12, 2007 @ 8:25 am

  3. err in the brackets should be ‘<ifmodule mod_rewrite.c>’ :(

    Comment by paul — June 12, 2007 @ 8:27 am

  4. @John – I’m happy that I’m not the only one to have come acropper with this. So far it seems to be holding out well for me – only reason I’ve needed to update my .htaccess file as been to ad some new referrers to ban.

    @ Paul – yep. Generally you wont need this.

    Rgds to both!

    Comment by Richard Hearne — June 12, 2007 @ 8:37 am

  5. Hey thanks for the advice…I’ve been having trouble with that too…hopefully this will fix the problem!

    Comment by Raymond — August 25, 2007 @ 3:30 am

  6. [...] I stumbled upon this as I was doing a bit of research, and hit upon some excellent posts here and here. [Please do read those posts before going ahead as they are excellent source of information] I had [...]

    Pingback by Stupendous Man » Separate Feedburner feeds and categories feeds using .htaccess — November 21, 2007 @ 4:20 am

  7. Hi Richard. I found this post thanks to a google search in hopes of fixing a wordpress/.htaccess issue I’m having. I wonder if you could shed some light on why my .htaccess file is preventing wordpress pingbacks from working.

    After messing around with my .htaccess I actually got a pingback to work once … once . Something in here is breaking pingbacks.

    Any insight you might have would be greatly appreciated.

    My rewrite rules are:

    # Use PHP5CGI as default
    AddHandler fcgid-script .php
    FCGIWrapper /usr/local/cpanel/cgi-sys/fcgiwrapper .php

    RewriteEngine On
    RewriteBase /
    RewriteRule ^feed/?$ http://feeds.feedburner.com/DavidMeade [QSA,L]
    RewriteRule ^itunes/?$ http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=87086557 [QSA,L]
    RewriteRule ^media/(.+)/?$ /post/post.php?mediaID=$1 [QSA,L]

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    Comment by David Meade — February 20, 2008 @ 4:14 pm

  8. Hi David

    Have to say I wouldn’t know where to start. You’re .htaccess is a little more exotic than most.

    Rgds
    Richard

    Comment by Richard Hearne — February 28, 2008 @ 7:57 pm

  9. Thanks for checking, Richard.

    I just last night got it to work. I’m actually not 100% sure what combination of things I was messing with finally got it to work. :-)

    Comment by David Meade — February 28, 2008 @ 8:33 pm

  10. um, how about you just chmod the .htaccess file to not be writable by the server? as in read only access?

    Comment by kenrick — June 2, 2008 @ 4:29 pm

  11. @Kenrick – simple answer to that is that WP cannot write to .htaccess if you chmod it. WP can actually write some useful stuff in there, and quite a few plugins use this functionality.

    Comment by Richard Hearne — June 3, 2008 @ 8:53 pm

  12. Thank you very much for this.

    It happened a few weeks ago and I assumed it was a server oddity that reset the .htaccess file.

    But today I found that saving any post/page caused the .htaccess to take out all my custom rules.

    I came here and it fixed it. Like you, I thought it made sense to only have 1 if statement rather than 2.

    Many thanks.

    Comment by James T — July 4, 2008 @ 3:57 pm

  13. thanks richard! fixed my problem!

    Comment by markus — July 6, 2008 @ 7:16 am

  14. thanks, it was getting silly this overwriting of my custom .htaccess ;]

    Comment by pkmk — July 23, 2008 @ 9:11 pm

  15. [...] Thanks to Richard at Red Cardinal for pointing out the very simple fix to this problem here. [...]

    Pingback by WordPress: Prevent WP from Overwriting Custom htaccess Rules | WhyPad — September 12, 2008 @ 4:24 am

  16. Ahhhh, thank you for saving me the trouble of trudging through the code.

    Comment by Phil Buckley — January 30, 2009 @ 1:43 am

  17. Brilliant! Solved all my headaches! THANKS!

    Comment by gee — May 25, 2009 @ 7:46 pm

  18. Wow, thank you. This has been making me CRAZY!!!

    Comment by Kevin — June 22, 2009 @ 4:33 pm

  19. Excellent. Thank you so much. And there was me thinking a plugin was at fault!

    Comment by David Robertson — December 4, 2009 @ 11:15 pm

  20. Hey, good tip. I was having the same problem. I just added my custom rules to the bottom. Everything has been fine since.

    Comment by Nick — December 18, 2009 @ 5:05 pm

Comments Feed TrackBack

Leave a comment