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;

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.

  • Sounds silly but It happens to me once, did you reload? $ sudo service nginx reload – Castiblanco Aug 28 '18 at 21:42
  • 1
    reloaded more times than i can count – pan Aug 28 '18 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? – Richard Smith Aug 29 '18 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 '18 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 '18 at 21:13

Your Answer

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

Browse other questions tagged or ask your own question.