3

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:

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.

2
  • What does your .htaccess look like? See serverfault.com/questions/206491/…
    – Dustin
    Jan 16, 2013 at 6:11
  • 1
    I suggest searching this site for nginx pretty permalinks, this has been asked and answered before.
    – Milo
    Jan 16, 2013 at 6:19

3 Answers 3

3

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.

4
  • You'll also want to make sure 'index index.php' is added inside the location rules.
    – Christiaan
    Apr 18, 2013 at 18:34
  • Awesome, I didn't have idea this actually existed. Just tried it and it's WAY more useful, since I just only have to remember to write it once (and it works for every wordpress site I have in the vps). May 11, 2013 at 3:09
  • Yup. It's a much more elegant solution than writing if statements, and more reliable.
    – Christiaan
    May 31, 2013 at 14:08
  • It will be more helpful if you can add where I can find this code in nginx setting. Like which file to check? nginx.conf? Jan 2, 2018 at 13:52
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.

2
  • i put the code in functions.php. all the post still can't available. Jan 17, 2013 at 1:10
  • Try resetting your permalink in the permalinks section. First change it to default settings, then switch back Jan 17, 2013 at 8:34
1

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;
}
2
  • 1
    I think it should be try_files $uri $uri/ /blog/index.php$args;. Lack of $uri/ will cause looping (e.g. /wp-admin address).
    – nmr
    May 1, 2019 at 11:14
  • @nmr you are correct. I have updated my answer. Thank you!
    – Chris H.
    May 1, 2019 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.