1

I have nginx running wordpress. Weird thing is the site won't 404 and instead rewrites the URL to the homepage. Any URL with a .php in the string will 404 correctly but /foo will not 404. Any help would be appreciated.

example.com/foo.php will 404

example.com/foo will not 404 and instead just loads the homepage.

    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    #try_files $uri $uri/ =404;
    }
# pass the PHP scripts 
#
location ~ \.php$ {
            fastcgi_intercept_errors on;
    include snippets/fastcgi-php.conf;
    # With php7.0-fpm:
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

error_page 404 = /404.html;
location = /404.html {
    root /home/web/error-pages;
    internal;
}

I've been able to narrow this down to my nginx (Ubuntu 16.04, PHP 7, MySQL) because even a fresh install of WordPress has the same behavior.

7
  • Sounds silly but It happens to me once, did you reload? $ sudo service nginx reload Aug 28, 2018 at 21:42
  • 1
    reloaded more times than i can count
    – pan
    Aug 28, 2018 at 21:45
  • 1
    The line try_files $uri $uri/ /index.php?$args; sends anything that isn't a static file to WordPress. Which is actually a desired behaviour, as it allows WordPress to see every request and implement pretty permalinks (if you have them enabled). Are you not seeing a WordPress generated "not found" page? Aug 29, 2018 at 8:30
  • Hi Richard, yes that is how i understand the 'try_files' line also. Wordpress is not generating the "not found" page for any request that should 404 instead it just loads the homepage with the incorrect URL. Not sure if it's a nginx thing or a WordPress thing.
    – pan
    Aug 29, 2018 at 18:44
  • Have you disabled all plugins and switched to a default theme, to ensure that a plugin or theme isn't "helpfully" redirecting 404s to the home page?
    – Pat J
    Sep 4, 2018 at 21:13

1 Answer 1

0

I did a test on a fresh install of WordPress 6.0.1, served using php-fpm 7.4.3 and nginx 1.18.0 on Ubuntu 20.04.4 using this nginx configuration:

server {
    listen 80;
    server_name wp.test;

    root /var/www/wp;
    index index.php;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }
}

(I also added to 127.0.0.1 wp.test to /etc/hosts).

Observation: accessing a URL that is supposed to be non-existent (e.g. http://wp.test/nonexistent) would show the contents of the home page (i.e. exactly the same content as http://wp.test/). This is unexpected, strange, and incorrect behavior.

I do not understand the cause of the behavior, but I found that it is possible to "fix" the problem by changing the permalink settings in the WordPress admin area. The problem only seems to affect the "Plain" permalink setting. The problem disappears when using another permalink structure. For example, with the "Post name" permalink setting, accessing http://wp.test/nonexistent would show a 404 error.

WordPress permalink settings

WordPress 404

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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