Creating a template for 410 errors

Community Forums Forums General Discussion Creating a template for 410 errors

This topic is: not resolved

This topic contains 5 replies, has 4 voices, and was last updated by  GermanKiwi 6 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #85079

    GermanKiwi
    Participant
    Post count: 15

    Hi all, I’m hoping someone might be able to help me with this question…

    I want to create a template file for using for 410 error messages in WP. I’m using the Redirection plugin which lets me assign either 404 or 410 errors to certain URLs, and in some cases I want to use 410 in order to tell Googlebot that the URL really is gone. (And I’m not asking for advice on why or whether I should use 410s – that’s not the scope of this post!)

    However, currently WordPress just uses the existing Genesis 404 template for these 410 errors. It does issue “410″ in the HTTP response, which is good, but the page itself is using the standard Genesis 404 template (from the 404.php file in the Genesis theme folder).

    I tried making a copy of the 404.php file (at /wp-content/themes/genesis), and naming the copy “410.php” – then I edited the text inside it to give it suitable wording etc. But I can’t figure out how to make WordPress use this file when giving a 410 error. So that’s what I’m hoping someone can help me with.

    I read in a very old post somewhere else, that you can add the following line to .htaccess and it will make WordPress use the 410.php file if it exists:

    
    ErrorDocument 410 /index.php?error=410
    

    …But I’ve tried this to no avail. And I know that I can use the “ErrorDocument” command in .htaccess to point to a regular .html or .php file, but I’d like to be able to use a proper WordPress template that will display my theme (with menu, logo etc) and look similar to my 404 page layout.

    So does anyone know if this is possible?

    Cheers!

    #97443

    Susan
    Moderator
    Post count: 9240

    As you posted this a while ago, and didn’t include a link to your site, I do not know if your issue is resolved. If it isn’t resolved, please come back and update this thread, and I can see if I can get some further assistance.

    If it is resolved, please mark the thread “resolved”, so that it can be closed.

    Have a great day!


    Susan @ Graphically Designing I offer theme customizations I tweet!
    I’ve taken up the challenge! – help me answer some of the unanswered posts

    #97552

    GermanKiwi
    Participant
    Post count: 15

    Hi Susan, thanks for replying! No, this issue is still not resolved, so I’d love to hear of any suggestions or a solution that you might have.

    The site in question is http://www.ibcstuttgart.de.

    As an example to test with: if you add /html to the end of that URL, you’ll get a 410 error (in the HTTP header) but the page will be using the Genesis 404 template. (I don’t want to provide a clickable hyperlink here to the 410 URL coz it may get indexed by Google from this forum, which I don’t want! So just add /html to the end of the domain and you’ll get it.)

    Thanks!

    #101461

    Len
    Participant
    Post count: 23

    Hi GermanKiwi,

    I don’t know if this is exactly what you are looking for but it’s an idea. You could use this plugin 410 for WordPress then create a 410.php template file to use in conjunction with it. This new template file could be as complex or simple as you want – even a slight variation of the Genesis standard 404.php file.


    #101464

    Tom
    Participant
    Post count: 936

    Hi,

    Just getting familiar with this … If you use Redirection to trigger the 410 response, could you also use 410 for WordPress to serve up your custom 410 page template?

    From their FAQ:
    "If you want to customise the message, just place a template file called 410.php in your theme folder, and the plugin will automatically use that instead. Take a look at your theme's 404.php file to see how the template needs to be structured. You can also hook into the wp_410_response action to trigger any specific events for queries resulting in a 410 response."

    Edit: I see Len has just beat me to the punch! .CA on the case :)


    Choose your next site design from over 350 Genesis themes.
    Updates via GenesisThemes.ca | Twitter | Google+ | Tumblr

    • This reply was modified 6 months ago by  Tom. Reason: Len's reponse
    • This reply was modified 6 months ago by  Tom. Reason: Len's reponse
    #101734

    GermanKiwi
    Participant
    Post count: 15

    Thanks for the suggestions regarding using that plugin!

    However, I’d really rather not use a plugin like that to try and accomplish this. The plugin provides functionality that I don’t want or need (eg. recording the URLs of deleted posts and pages etc), and in any case I don’t want to install a whole plugin just to provide the functionality of a 410 template – I’m sure it should be possible to achieve what I want using a template file and/or a function in my functions.php file.

    Here’s the function that I already tried to use (along with a corresponding 410.php file in my Genesis child theme directory):

    function error_410( $template ) {
        if( is_404() && '410' == $_SERVER['REDIRECT_STATUS'] ) {
            status_header( 410 );
             if( file_exists( get_stylesheet_directory_uri() . '/410.php' ) ) {
               return get_stylesheet_directory_uri() . '/410.php';
            }
        }
         return $template;
    }
    add_filter( 'template_include', 'error_410' );

    However, it doesn’t work (when tested against a test URL I created using the Redirection plugin which is set to give the 410 status).

    From my troubleshooting, I believe the problem is with this bit:

    $_SERVER['REDIRECT_STATUS'] )

    As far as I can tell, this isn’t triggering on my test URL at all. If I use http://web-sniffer.net on this URL, it does show that the HTTP status is 410, so I know that part is working. And I have also been able to confirm that the “if( is_404())” part of the function works fine. So I believe the problem is with the above bit of the function.

    Is there an alternative, or better, way to detect the HTTP status code of a given page, using a WordPress function? I think if I could get this bit of the function working, then the rest of it might just work too.

    • This reply was modified 6 months ago by  GermanKiwi.
    • This reply was modified 6 months ago by  GermanKiwi.
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.