WordPress.org

Ready to get started?Download WordPress

Forums

How to generate 301 error for moved page? (4 posts)

  1. deko
    Member
    Posted 4 years ago #

    I have a very basic Page.php template:

    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
         <div class="post" id="post-<?php the_ID(); ?>">
              <h2><?php the_title(); ?></h2>
              <div class="entry">
                   <?php the_content(); ?>
              </div>
         </div>
         <?php endwhile; ?>
         <?php edit_post_link('Edit', '<p>', '</p>'); ?>
         <?php else : ?>
         <h2>Not found</h2>
    <?php endif; ?>
    </div>
    <?php get_footer(); ?>

    I recently moved some Pages that search engines are still generating links to. Is there a wordpress template tag I can use to replace the "Not found" message with a HTTP 301 Moved Permanently error?

    Or do I have to write code to identify the page_id of the moved page and use the PHP header() function?

  2. deko
    Member
    Posted 4 years ago #

    I tried this...

    $pid = trim($_GET['page_id']);
    if ($pid = 362)
    {
         header("HTTP/1.1 301 Moved Permanently");
         header("Location: http://www.example.com/MovedPage.php");
    }

    but received error: "Can't rewrite headers... already set"

  3. deko
    Member
    Posted 4 years ago #

    I've noticed this behavior isn't unique to my blog.

    To duplicate, go to any wp blog and add ?page_id=23423432434 to the end of the url, like this: http://www.exampleWPblog.com/?page_id=23423432434

    If you try this on blogs listed in the Your WordPress forum, for example, you will get a blank page. That's what I was getting before I added the <h2>Not found</h2> bit shown in my previous post.

  4. deko
    Member
    Posted 4 years ago #

    I'll just wait for the search engines to catch up and stick with the "Not found" message. the 404.php file is useless. delete it.

Topic Closed

This topic has been closed to new replies.

About this Topic