Display a Wordpress 404 page in a subdirectory with its own 404 page

In a subdirectory, for example http://website.com/sub/index.html , which has its own .htaccess, for some reason still shows the 404 page that I have for Wordpress, which is located in the directory above this.

.htaccess for sub dir:

ErrorDocument 404 /404.html 

.htaccess for the main directory:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^website.net$ [OR] RewriteCond %{HTTP_HOST} ^www.website.net$ RewriteRule ^foghelper.php$ http://website.net/subdirone/ [R=301,L] RewriteRule ^foghelper.php$ http://website.net/subdirtwo/ [R=301,L] # DO NOT REMOVE THIS LINE AND THE LINES ABOVE jNLK5d:REDIRECTID # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

Links that were in hta were censored if you were wondering.

Edit:

After adding RewriteCond %{REQUEST_URI} !^/?sub/ to RewriteRule . /index.php [L] RewriteRule . /index.php [L] error still exists.

+4
source share
2 answers

Everything is laid out through wordpress, so when a request for something like /sub/no_file.html and it does not exist, wordpress routes it through its index.php , because it satisfies the conditions !-f and !-d (and not the existing one file, not an existing directory). He then decides that he cannot find the resource for /sub/no_file.html , so he displays his own 404 file.

If you want requests that go to / sub to bypass wordpress routing, so that if a file is requested in /sub/ that does not exist, /404.html will be serviced, you can add this line right before the RewriteRule . /index.php [L] RewriteRule . /index.php [L] in your wordpress rules so they look like this:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/?sub/ RewriteRule . /index.php [L] </IfModule> 
0
source

The default .htaccess file will support your behavior;

 # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

The magic is in the lines starting with RewriteCond. They instruct Apache to apply the RewriteRule./index.php [L] rule (which means that "any URL will be sent to index.php") only if the URL is not an existing file! -f or an existing directory! -d.

So this should work by default. Wordpress rewriting rules do not apply when you try to visit an existing file.

0
source

All Articles