سؤال

I have a new WP website in stage (https://cpanel-511-syd.hostingww.com/~bcctovkp/) that I need help with the functioning of the site when you try to navigate to a page or file not defined within this WP site.

There are two separate (but related) issues.

The 1st one is strange:

If you try to navigate to this invalid URL: https://cpanel-511-syd.hostingww.com/~bcctovkp/a/b/test.gif, or this one: https://cpanel-511-syd.hostingww.com/~bcctovkp/a/b/c/ you will get a page that displays all the [full length] posts, one right after the other on one long page.

The sub-directories a, b and c are non-existent, and so is the test.gif file. It doesn't matter what you enter as an invalid path, as long as you have at least THREE sub-directories or TWO sub-directories AND a file, you will get this long posts page.

If you try to navigate to any other invalid URL like this: https://cpanel-511-syd.hostingww.com/~bcctovkp/a/test.gif or this: https://cpanel-511-syd.hostingww.com/~bcctovkp/a/b/, you will get the standard "Oh no! No content is appearing for this page!" message displayed on the page instead.

I would like to figure out what causes the long posts page to display sometimes instead of the "Oh no" message. (And bonus points if you can tell me the default WP page that creates the long post page.)


Second issue isn't really a problem, but more a question of WP functionality:

Ideally, I would like to get my 404.php page to display when any visitor enters any invalid URL on my domain (instead of the getting the "Oh no" message).

I have created a test 404.php page, which can be found here: https://cpanel-511-syd.hostingww.com/~bcctovkp/404.php. It is located in the root directory.

I think WP allows you to use your own 404.php page? If so, where should it be placed, and how do you activate the 404 page?

Thanks for any help you can provide.

SunnyOz

هل كانت مفيدة؟

المحلول

So yes, the key point here is that you should put the WordPress-specific 404 template file in the theme directory (e.g. wp-content/themes/your-theme/404.php) and not in the root WordPress directory.

And despite that you already got the solution, I thought you would still want an answer to the following?

I would like to figure out what causes the long posts page to display sometimes instead of the "Oh no" message.

So in a default WordPress setup with %postname% as the post permalink structure, the first two requests (/a/b/test.gif and /a/b/c/) do not match any of the (saved) WordPress rewrite rules, hence a 404 error is issued, but WordPress would still query the recent posts on the site — and this query is what we call as the main (WordPress) query.

And when the WordPress-specific 404 template is not available, the default index template (index.php) is used and therefore you'd see the long posts page because the index template normally displays posts from the main query mentioned above.

But the other two requests (/a/test.gif and /a/b/) matched an attachment post request (i.e. there are rewrite rules for those requests/URLs), so WordPress would try to find the attachment post (test-gif and b respectively), and if not found, then the "no content" message would be displayed instead. See have_posts() and this part in Twenty Twenty-One's index template.

So the rewrite rule defines the query and the query determines the template that should be used, and index.php is the default template used when all other matching templates (404.php, category.php, etc.) are not available — see the WordPress template hierarchy for more details.

نصائح أخرى

Thanks to @SallyCJ for setting me on the right track.

In regards to the 1st issue... the page that is being displayed is my index.php file. (The default Wordpress page displays all posts, one after the other.)

It still isn't clear why some invalid URLs display this page, and others didn't. I guess we will never know.

But it is not really important anymore, as I found out (from the link that @SallyCJ provided in the comments) that my 404.php file should be placed in the template folder (not in the WP root directory).

I have moved the 404.php file there now, and everything works like I wanted it to.

(Note: So now the above invalid URLs in my question will no longer display the index.php page or the "Oh no" message - as it did when I first posted this question.)

Thanks for your help.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top