• Resolved Bramernic

    (@bramernic)


    OK, I (and several others) have been encountering strange behaviour when trying to access a known missing page on my site. I have spent several hours researching what exactly is happening and I’ve even waded through some of the code to see if I can find the issue. Unfortunately I can’t. I firmly believe this is a bug within WordPress and I’d appreciate knowing how to bring this to the attention of the developers if posting in here isn’t enough.

    What Happens
    When the 404.php template is called to announce a missing page in a friendly manner you actually get what looks like utter garbage. It is in fact the output of 404.php, but it is missing the first 10000 or so characters. Thus what you see is raw HTML. This only happens when you are using permalinks. The exact number of characters missing depends on which form of permalinks is being used, but it is always the same number whenever the page is called with the same permalink system.

    What I’ve done to trace the error

    1. Copied 404.php over page.php and verified that the code does do what it should.
    2. Added sufficient text to 404.php so that I could be sure that it is actually being used.
    3. Found the point within 404.php where output starts and copied the first few lines (before my spurious text) to that point. This now displayed a basic page without any CSS formatting.
    4. Took out the call to get_header() and replaced it with the contents of header.php. This actually worked!!!

    What now?
    I have a file that appears to work with my chosen permalink structure, but it contains an awful lot of garbage. As long as I don’t encounter problems with further testing then I’m happy, but, I feel it really ought to be fixed properly. Let me know if you (ie a developer) wants any further information.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Have you tried:

    – deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    – switching to the default theme to rule out any theme-related problems?

    Can you paste your 404.php code here? also is that possible to provide the URL so we can see the messy result?

    Thread Starter Bramernic

    (@bramernic)

    Yes, sort of.

    I only have 1 plugin activated [not counting Hello Dolly ;)], but I have more than one site with the same problem and another site doesn’t have that plugin. It’s Improved include page by the way.

    I did also switch to the default theme and the same issue occured. I meant to mention that above, sorry.

    Thread Starter Bramernic

    (@bramernic)

    @hfort: Here is the code from the original 404.php file:

    <?php get_header();?>
    <div id="wbody">
    <div id="content">
    <div class="post">
    <h2 class="posttitle">Error 404 - Not Found</h2>
    	<div class="entry">
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	</div>
    </div>
    </div> <!-- content -->
    <?php get_sidebar();?>
    <?php get_footer();?>

    The site I’ve been playing with I’d prefer not to put the URL in the public domain just yet. However, the other site is https://www.desirephotography.co.uk/blog. Just add any garbage to the end. I will probably be adding my ‘fix’ soon to that site as it is one of my clients.

    That looks like part of your sidebar template – so you might want to have a closer look at sidebar.php.

    Thread Starter Bramernic

    (@bramernic)

    @esmi: Yes it is part of the sidebar template because that is where it gets up to before starting to output. When I added lots of extra stuff in 404.php I saw more of the sidebar code, then eventually the extra stuff in 404.php. This is how I know it isn’t a problem with any of my template files. Remember, I also copied 404.php over the top of page.php and when I tried to display a page it showed me a properly formatted 404 page.

    That is weird. However, you are missing a closing div.

    Try this code which is based on your source code and report the output to us.

    <?php get_header();?>
    <div id="wbody">
    <div id="content">
      <div class="post">
        <h2 class="posttitle">Error 404 - Not Found</h2>
        <div class="entry">
          <p>Sorry! but the page you requested has either been deleted or does not exist.</p>
        </div>
      </div>
    </div>
    <!-- content -->
    <?php get_sidebar();?>
    </div> <!-- was missing this div to enclose the content and sidebar -->
    <!-- wbody -->
    <?php get_footer();?>
    Thread Starter Bramernic

    (@bramernic)

    @hfort: The closing div is actually the first line in footer.php. I didn’t write the original theme, although I’ve made significant changes since. It was just a very simple theme called white boxes to get me started.

    Thread Starter Bramernic

    (@bramernic)

    I can see I need to post the file that works so you can see how ridiculous it is. Apologies in advance for the length of the code box.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
    <head profile="https://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    <title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> &raquo; Blog Archive <?php } ?> <?php wp_title(); ?></title>
    <link rel="shortcut icon" href="/favicon.ico" />
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    <?php wp_head(); ?>
    </head>
    <body>
    
    <div id="wrapper">
    <div id="header">
    <a href="<?php echo get_option('home'); ?>"><img src="/images/blog_logo.jpg"></a>
    </div> <!-- header -->
    <div id="wbody">
    <div id="content">
    <div class="post">
    <h2 class="posttitle">Error 404 - Not Found</h2>
    	<div class="entry">
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>Sorry! but the page you requested </p>
    <?php get_header();?>
    <div id="wbody">
    <div id="content">
    <div class="post"><div class="topcorn">
    <h2 class="posttitle">Error 404 - Not Found</h2>
    	<div class="entry">
    	<p>&nbsp;</p>
    	<p>Sorry! but the page you requested has either been deleted or does not exist.</p>
    	<p>&nbsp;</p>
    	<p>&nbsp;</p>
    	<p>&nbsp;</p>
    	</div>
    </div></div>
    </div> <!-- content -->
    <?php get_sidebar();?>
    <?php get_footer();?>

    As that site should no longer exhibit the problem, here is the url of another site that does: https://www.carolinepilling.com/. This one is a completely different theme written by another developer.

    Hi Bramernic, to single out the issue. (I don’t know if you have done this before, but…).

    1- Change the theme to the default theme, and test it for the 404 result.
    a) If it works. Then…
    – It is not the WordPress.
    – It is not the Hosting.
    – the .htaccess does not have any special handler for the 404s
    – And finally we know the problem it is on the specific theme.

    b) If it doesn’t work and get the same messy result. Then…
    – It may be the hosting.
    – Special handler on .htaccess for 404s
    – WordPress install.
    – and finally we know the problem is not on the theme.

    Possible solutions for 1. (other themes work).
    1- Check the function.php for any special function mentioning 404.
    2- open header.php and remove <?php wp_head(); ?> test it and place it back after test.
    3- rename 404.php to something else. open page.php, rename it as 404.php, remove the loop, place the error message, save it, upload it and test it.

    possible solutions for 2 (any theme doesn’t work)
    – check the .htaccess to see if there is anything about 404s
    – Leave us to check the wordpress install and hosting.

    What is the theme you are using?

    Thread Starter Bramernic

    (@bramernic)

    I’m using an amended White Boxes by Dr Oguz. I have already tried other themes including the default one and they all fail in the same way. Here is the .htaccess file:

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

    The hosting I am using is Fasthosts.

    The .htaccess is fine. It seems to be related to your hosting then.
    If you used the one click install, then maybe the hosting has a problem with the installation script. Also, the hosting itself may have its own way of handling the 404 and it’s conflicting with wordpress.

    Lastly, you may try this plugin https://www.ads-software.com/extend/plugins/smart-404/ but you have to remove the old 404.php

    Thread Starter Bramernic

    (@bramernic)

    Thanks for those thoughts. The smart-404 plugin looks interesting, although it seems to work in conjunction with 404.php so might still exhibit the problem. I’m going to contact my hosting tech support to see whether they are doing anything that conflicts with WP. It does seem strange that everything else works though.

    Final note to anyone thinking of using my workaround above. On one of the two sites that I put it in last night it now skips fewer characters this morning so the fix had to be adjusted. I’m not sure what the extra variable is that caused this change, but it certainly points towards the hosting being at fault. I guess it might be a timing issue, which will be almost impossible to resolve.

    Thread Starter Bramernic

    (@bramernic)

    Update:

    Thanks all for your help, especially Hfort. It turns out that 404 handling is the one feature of WordPress which is not compatible with the shared hosting platform that I use. Marvelous!

    The workaround that I am now developing involves using a cut-down version of the smart404 plugin to redirect all 404s to a specific page number (set in options) using 301 instead. It’s not ideal, but it does avoid displaying a page full of HTML. To stop the page being included in wp_list_pages I’ve made it private, but that also required a change to the page template file for the title (to stop the pre-pending of “Private: “).

    I’ll consider making this plugin available once I’m happy it is as straightforward as it can be.

    Thread Starter Bramernic

    (@bramernic)

    Plugin No 404 Errors now available.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘404.php not working with permalinks – extensive debugging carried out’ is closed to new replies.