Categories
Posts

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.