Index.php = 404 Error (rtCamp has this issue too)

Hi there folks,

The default behaviour of WordPress is to redirect to “/” when there’s “/index.php” at the end of the URL. Since 3.7 WordPress is automatically recognizing Nginx and enables “pretty” permalinks. Plus Nginx Helper plugin should be able to remedy this.

However “nginx+wordpress (+fastcgi_cache) (+W3TC)” is producing 404 error when “index.php” is appended at the end of the URL. Valid redirection (stripping the “index.php”) only happens on the homepage.

This is the case with many websites out-there that use Nginx, rtcamp included. For example take a look at these pages with “index.php” at the end. For the sake of the experiment also try their homepages with “index.php” appended.

https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/index.php

https://danielmiessler.com/blog/ultimate-speed-wordpress-nginx/index.php

http://www.makeuseof.com/tag/minix-z64-windows-edition-review/index.php

They all produce 404 error but they should redirect to a clean version of the same URL. What is surprising is that the last website uses W3TC and still has this problem.

However not all websites on Nginx are the same. Check these URLs for example. They behave as they should.

https://yoast.com/how-to-clean-site-structure/index.php

https://deliciousbrains.com/hosting-wordpress-yourself-nginx-php-mysql/index.php

Now I have the same problem and I was trying to solve it but I came up with one ugly solution that causes even more problems.

Putting:

if (!-e $request_filename) { rewrite .*$ /index.php last; }

before:

location ~ \.php$ { try_files $uri =404; ........ }

solves the problem but messes with my other custom redirection rules which I was shuffling around and still couldn’t make them right. Plus we all know that IfIsEvil.

I guess the key pieces of code in solving this problem are:

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

and:

try_files $uri =404;

As far as I understand, for some reason the first one is not identifying the “index.php” in the URL (except for the homepage - “/”), thus the second one is producing 404 error when “index.php” file is accessed directly.

Elegant solution for this problem would be appreciated.

Can’t believe this got buried so quickly. It concerns rtCamp too. Bump.

@vlatan

You can modify the location ~ \.php$ like below to get it working as you need.

location ~ \.php$ {
# Before       try_files $uri =404;
        try_files $uri /index.php;
        .
        .
        .

}

Thanks @harshadyeola. It appears your suggestion solves the problem but

try_files $uri =404;

is a security feature. You don’t want Nginx to execute other files as php. You either use that or

fastcgi_split_path_info ^(.+\.php)(/.+)$;

or both along with

cgi.fix_pathinfo = 0 set in php.ini.

There’s a question of the same nature at serverfault and one of the answers suggests that you can use any of those. But can you really?

So what’s proper security feature to use and still retain your solution above?

One other issue is that if you’re already doing ‘www’ to ‘non-www’ redirection now you’re introducing a second redirection.

For example "http://www.example.com/index.php" will redirect to "http://example.com/index.php" and then there will be another redirection to "http://example.com/".

Wordpress on Apache handles this exact same situation in one single redirection. "http://www.example.com/index.php" to "http://example.com/".

Anyone? Bump.