WordPress, Permalinks, Mod_Rewrite, and Avoiding 404s

I made a mistake in changing my WordPress permalinks, but by the time I’d discovered it my blog had already been indexed. Fixing the permalinks meant breaking those indexed URLs, leading to a bad user experience, but leaving them as is wasn’t really an option.

Last night, after getting 404’d while using Google to search my own blog, I realized I had to do something.

First I looked at Apache mod_rewrite and the URL rewriting guide (as well as this cheat sheet from ilovejackdaniels), Then, frustrated, I found some items in the WordPress Codex, including this one about conflicts between .htaccess files/rules and this about problems with subdirectories inside the WordPress root.

The suggestions in both boil down to creating a specific error document:

Create a file on your website. Call it onerror.html. It can be empty or have just <html></html> in it for all that it matters.

In WordPress’s .htaccess file, add this to the top of the file:
ErrorDocument 401 /path/to/onerror.html
ErrorDocument 403 /path/to/onerror.html

The explanation, though even the poster doesn’t claim to know that it’s the correct explanation, is that the rules are additive, and WordPress is greedy with trying to get requests directed to its own URL parsing routines. The ErrorDocument is supposed to keep more of the processing in mod_rewrite.

But after all that, and only limited success achieving what I wanted, I realized that what I really wanted was not to parse requests before WordPress had a chance at them (as would happen with rewrite rules), but to handle requests that WordPress’ internal rules couldn’t match.

What I ended up doing is writing a tiny plugin that hooked into the template_redirect hook. The short of it is that if WordPress thinks it’s 404’d, and I can divine some meaning from the requested URL, then I redirect to browser (including a HTTP/1.1 301 Moved Permanently statement). Voila, it works.

wordpress, rewrite, mod_rewrite, rewrite rules, apache rewrite, permalinks, redirect, 404

11 thoughts on “WordPress, Permalinks, Mod_Rewrite, and Avoiding 404s

  1. Can you explain a little bit more about what you did or show some code or anything?
    I’m really interested because I’m having same kind of 404’s errors.

    Thanks in advance.

  2. Hi,

    I would like to know the code what you implemented to redirect 404 by not invoking any query, as my site is going down often due to some ips accessing non existent urls. if you can provide me the code it would be a great help for me.

  3. Pingback: Think Bohemian

  4. Pingback: WordPress-404-Meldungen bei geänderten Weblog-Posting-URLs abfangen – Magerquark.de

Comments are closed.