0

Whenever I call a non-existent page, say http://www.example.com/foobar/, it should invoke the 404.php theme template file.

In my case, however, the site redirects to the homepage, /

I'm not sure what I did in the past to enable this.

How can I ensure that 404.php takes precedence when a file is non-existent?

FYI:

My .htaccess file is below...

I'm not sure where the first two IfModules came from. I recently tried and disabled caching plugins. I have gone to the Permalinks settings and re-saved.

I am running Multisite. The site is the primary site in question. And I may need to retain the ability to power multiple sites off it.

Removing...

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

... doesn't work.

Removing the entire portion after # BEGIN WordPress results in a server-level 404: "Not Found. The requested URL was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

# BEGIN LSCACHE
# END LSCACHE
<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} ^/?wp\-content/+debug\.log$
        RewriteRule .* - [F,L,NC]
</IfModule>
<IfModule !mod_rewrite.c>
    <Files "debug.log">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order deny,allow
        Deny from all
    </IfModule>
    </Files>
</IfModule>


# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>

# END WordPress

# Modified for Multisite https://www.namecheap.com/support/knowledgebase/article.aspx/9191/29/how-to-create-a-wildcard-subdomain-in-cpanel
# Previous here:
# RewriteEngine On
# RewriteBase /
# RewriteRule ^index\.php$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]



# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
</Files>

# END Wordfence WAF

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 1000
   php_value max_input_time 1000
   php_value max_input_vars 8000
   php_value memory_limit 1024M
   php_value post_max_size 128M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php70"
   php_value upload_max_filesize 128M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php70” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php70 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
0

In my case, the issue was the multi-site config attribute NOBLOGREDIRECT

This had been set in wp-config.php...

// Redirect non-existent Multisite subdomain calls to www primary site
define( 'NOBLOGREDIRECT', 'http://www.example.com' );

This prevents a valid 404 appearing since it is always interpreted as a bad call to a subdomain.

There are two solutions:

  1. Comment it out
  2. As stated by @brij's post (discovered through Google), override it:

In wp-config.php, NOBLOGREDIRECT is defined so that when someone enters a subdomain that does not exist on your site to redirect to whatever url you wish it to. But the bad thing is when someone tries to access the page of domain that doesn’t exist, it redirects to NOBLOGREDIRECT defined location instead of throwing 404. To fix it, add following PHP code in functions.php or any related file

<?php remove_action( 'template_redirect', 'maybe_redirect_404' ); ?>

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.