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

Related:

7 Comments

  1. Comment by jessamyn on April 20, 2007 1:44 am

    hot damn, I would like to see this.

  2. Comment by Casey on April 20, 2007 10:46 am

    See it? It’s in use now.

    Examples

    The URL to this post
    http://maisonbisson.com/blog/post/11617/

    Some broken URLs to this post:
    http://maisonbisson.com/blog/post/11617/wordpress-permalinks-mod_rewrite
    http://maisonbisson.com/blog/post/11617/asdasdasd

  3. Comment by Mojo on May 9, 2007 12:39 am

    Can you publish your code? I’m interested in this as well.

  4. Comment by Yo on June 5, 2007 10:41 am

    i need please a username n’ password of google earth pro

  5. Comment by kike on January 11, 2008 10:46 am

    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.

  6. Comment by smartdesis on January 19, 2008 9:22 pm

    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.

  7. Comment by Casey on January 21, 2008 9:21 am

    @smartdesis: the method discussed here likely creates additional load on the server for 404s. You might, however, be interested in this discussion: http://maisonbisson.com/blog/post/12035/wordpress-invalid-urls-extra-database-queries

Comments RSS TrackBack Identifier URI

Leave a comment

 

User contributed tags for this post:

1