How to remove the index.php in the url?

I have installed wordpress in a subdirectory (blog). In the root directory I have installed Magento. Now the file directory is as following:

app
downloader
includes
media
....
blog/wp-admin

My server is nginx. When I set the URL in the wordpress Permalink Settings to:

Read More
http://www.example.com/blog/index.php/sample-post/ 

… all the posts are available. Now I want to remove the index.php from that URL. How should I do that? When I set the custom structure to …

http://www.example.com/blog/sample-post/

… the URLs are not resolved by WordPress and I get a 404.

Related posts

Leave a Reply

3 comments

  1. The problem likely has to do with the server settings in Nginx for your blog. It’s likely that the location rules for /blog/ are wrong, specifically try_files. It should look like this:

    location /blog/ {
         try_files $uri $uri/ /index.php$is_args$args;
    }
    

    This tells Nginx the order in which it should try to find the requested resource. It will first start by trying to find the exact object in the URL. If that doesn’t exist, it’ll try to find that object as a directory. If that doesn’t exist, it’ll pass the request to index.php along with any arguments or query strings, if present. Since index.php in the main handler for WordPress, this will trigger WordPress to find the page or post you’ve requested based on your rewrite rules.

    The beautiful thing about this configuration is that you can now change your permalink structure to anything WordPress supports without having to change your Nginx rules. This is also the recommended method over using explicit rewrite rules in Nginx.

  2. Place this code in your functions.php file

       add_filter( 'got_rewrite', '__return_true', 999 );
    

    Update:

    got_mod_rewrite() function checks whether the current server is apache or not using apache_mod_loaded() function. Since we are using nginx it returns false. So wordpress adds index.php in the url.

    By using got_rewrite filter we are telling wordpress that we got rewrite in our server.

  3. Add this location object to your server configuration in your nginx.conf.

    location /blog {
        try_files $uri $uri/ /blog/index.php$is_args$args;
    }
    

    Then restart nginx.

    Symbolic Linking to WordPress folder

    I’m using a symbolic link to the wordpress folder in my implementation and I found there are still complications when trying to access real folders inside of that symbolic link. There are three of these folders and the try_files $uri/ part seems to fail here (at least for me), so I ended up manually excluding those folders from being manipulated by try_files. If you are having similar issues, try the location block below.

    location ~* /blog/(?!.*(wp-admin|wp-content|wp-includes)).* {
        try_files $uri $uri/ /blog/index.php$is_args$args;
    }