Home > Blogging, Computers, Howto > WordPress 404 Error when updating or posting

WordPress 404 Error when updating or posting

It’s time for a new icon! This is my official “Sherlock Holmes deerstalker hat” icon, which is reserved for when I have to do some strange detective work in order to solve a baffling problem. Looking at it again, it looks a bit like a snail, but trust me – it’s a deerstalker!

Since moving from a free blog hosted on WordPress.com to my self-hosted WordPress blog, everything has been going very smoothly. I have had more control, and been able to insert some Google Ads into popular posts, which should just about cover my hosting costs. Things were going swimmingly until I decided to update one of my posts and immediately got a 404 Not Found error message. Adding new posts seemed to be fine and, to add to the conundrum, some posts would let me update.

A lot of the forums I read indicated that this was a problem with mod_security rather than WordPress itself. So what is mod_security? It’s a web application firewall that works either embedded into Apache (the webserver), or as a reverse proxy. Its job is to filter out attempts to compromise your site, such as cross-site scripting or SQL injection attacks. This is a good thing.

The WordPress issue arises, though, because the mod_security rules run on POST, i.e. when you click the Update button. If your blog post contains text or characters that violate the mod_security rules, then you’ll get a 404 Not Found error page. Fear not, for there are ways to get around this. Unfortunately, to continue the Sherlock Holmes analogy, they’re not “elementary, my dear Watson”.

The Ideal Method

The first way to prevent this error is to stop mod_security from scanning POST commands. If your server, or hosting provider, is running mod_security 2.5 or above, then skip this section. In older versions of mod_security you can override the settings using a file called .htaccess which is in the root directory of your blog. Simply add the line:

SecFilterScanPOST Off

to the .htaccess file, and all should be well. If you’re running a newer version of mod_security then you can exclude some files in the wp-admin directory from the filter. You will need access to the system files on your web server, or your friendly hosting provider might add these rules for you.

Add the following rules to the /usr/local/apache/conf/modsec2/whitelist.conf file:

<LocationMatch "/wp-admin/post.php">
SecRuleRemoveById 300015 300016 300017
</LocationMatch>
<LocationMatch "/wp-admin/admin-ajax.php">
SecRuleRemoveById 300015 300016 300017
</LocationMatch>
<LocationMatch "/wp-admin/page.php">
SecRuleRemoveById 300015 300016 300017
</LocationMatch>

Again, this will solve the problem. However, what if you don’t have access to your server or can’t get your hosting provider to add the changes?

The Workaround

If you can’t do the above fix, then the only option is to modify the content of your blog post, so that it doesn’t look like a potential attack.

The posts I wanted to update contained phrases like “select restore from backup”, “delete everything from…”. If you’re savvy with SQL, you’ll know that they look like SQL commands. It seemed that mod_security was being a little over-zealous and thought that I was trying to run a SQL injection attack! Therefore I was presented with the 404 page and prevented from proceeding.

You can work around this. Avoid using words like “select” or “delete” particularly if they’re followed by the word “from”. If you must use them, then disguise the words by substituting letters for their HTML codes using this table. So switch to HTML view in the WordPress editor and change occurrences of the word “select” to “&#115;elect”, “delete” to “&#100;elete” and so on.

It’s not pretty, but it works. Remember as well that if you switch back to Visual view, then your HTML codes will be lost! It’s good that we can workaround this problem, but I wonder how long it will be before mod_security is updated to prevent the use of ASCII codes in this way.

  1. October 27th, 2010 at 00:34 | #1

    My client just had the same problem with one of her posts. Thanks for a concise write up of this issue.

    cheers
    Anthony

  2. October 27th, 2010 at 08:38 | #2

    You’re welcome – glad it helped. Thanks for taking the time to comment, the feedback is appreciated :)

  3. November 4th, 2010 at 01:43 | #3

    Just helped another designer friend of mine who had a problem with this. Go figure that this was the problem. Thanks for this! Bookmarked it for sure.

  4. November 4th, 2010 at 08:08 | #4

    You’re welcome. It’s an odd problem for sure, but glad you got it sorted. Thanks for taking the time to comment :)

  5. August 27th, 2011 at 18:08 | #5

    I’ve got strange problem after install mod_security, the problem was I cannot save my widget’s setting, what must i exclude in mod_security setting to fix it.

  6. April 25th, 2012 at 07:19 | #6

    Looking smart and brilliant :D

  7. July 17th, 2012 at 21:52 | #7

    I Just want to thank you, this 404 error has been haunting me ever since we started using mod_security. Allot of the information out there has people trying to disable in the htaccess file. The whitelist.conf edits worked perfect when all else failed!

  8. July 17th, 2012 at 22:51 | #8

    You’re welcome :) thanks for taking the time to comment

  9. Josh
    November 1st, 2012 at 11:12 | #9

    Thank you so much! This is the only method I found that worked (modsec2)

  1. December 5th, 2010 at 16:28 | #1
  2. October 21st, 2011 at 15:07 | #2
  3. July 9th, 2012 at 14:09 | #3