WordPress Pretty Permalinks with Nginx

Setting Permalinks in WordPress is simple and flexible. Core WordPress checks to see if the Apache mod_rewrite module is loaded before fully enabling permalinks. If that check fails then it includes index.php in your permalink structure in order to make sure that WordPress still gets a chance to process URLs.

Servers using Nginx are able to do URL rewriting, but the check still fails, leaving index.php in the permalink. Fortunately a few lines of PHP in a plugin (I like to put it in wp-content/mu-plugins/nginx.php) can fix that:

[sourcecode lang=”php”]
add_filter( ‘got_rewrite’, ‘nginx_has_rewrite’, 999 );
function nginx_has_rewrite( $got_rewrite ) {
return TRUE;
}
[/sourcecode]

Those few lines of PHP will tell WordPress that full URL rewriting is available on the server, so no more index.php in the permalink structure.

Before adding this make sure that you’ve already setup URL rewriting in your Nginx configuration.

Update: In the comments Sivel mentioned another alternative:

[sourcecode lang=”php”]
add_filter( ‘got_rewrite’, ‘__return_true’, 999 );
[/sourcecode]

The __return_true function was added during WP 3.0 development for exactly these sorts of things, where you want to just return TRUE for a filter. I like it.

20 Comments

  1. You can reduce that a little by using the following instead:

    add_filter( ‘got_rewrite’, ‘__return_true’, 999 );

  2. You have curly quotes in your copy of Matt’s code. 😉

  3. Ah, I need to beware of copy and paste results 🙂

    Nice catch, fixed.

  4. I use nginx on my server, and I’ve never had this problem with permalinks.

  5. I saw someone else mention this, it may be something that happened as part of the 3.0 development cycle.

  6. You are correct, this something that was introduced as part of the 3.0 development cycle when bringing in WPMu. If you never touch your permalink structure after the upgrade to 3.0 you should be fine, but without adding this code it could introduce index.php into your permlaink structure.

  7. That’s consistent with what I’ve seen so far.

  8. Me too never had problem with this.
    I am running 2 big MU’s with domain mapping and most blogs have their own permalink structure.
    But with reference to other comments, if this is an issue with 3.0, thanks a lot Joseph in advance

  9. I simply comment out line 92 and 93 in the wp-adminoptions-permalink.php file

    if ( ! got_mod_rewrite() && ! $iis7_permalinks )
    $prefix = ‘/index.php’;

  10. I strongly recommend NOT editing core WP files, too easy to loose or trash your changes during an upgrade.

  11. In my WordPress was a some modification, and if core wordpress upgrade, I merge the upgrade to my SVN repo.
    So I don’t loose this modification 🙂

  12. I didn’t say it was impossible to keep them, but it is much less likely. If it can be done via a plugin it should be done via a plugin (in most cases).

  13. Thanks so much for this.

  14. Awesome, thanks a ton for this. I know this is an old post and all, but is there any way to implement this directly into the vhost config with nginx? That way you dont have to add this file as a plugin etc?

  15. Changing the Nginx config won’t impact this. Best you could hope for was to get an update in WordPress that would make this an option. Outside of that you’ll need to stick with the simple plugin.

  16. Currently I am using Nginx Compatibility plugin for this purpose..But want to replace that with this code..Can I place above code in functions.php file of my theme..?

  17. I haven’t specifically tried that, but could work.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2019 Joseph Scott

Theme by Anders NorénUp ↑