WordPress.org

Ready to get started?Download WordPress

Forums

404 Redirect (12 posts)

  1. njras5
    Member
    Posted 2 years ago #

    I've tried all kinds of ways to redirect the 404.php file in WordPress versions 3.3.1 and 3.3.2 to the home page, but nothing seems to be working for me. I've tried plug ins, and my latest attempt, replacing the code in the 404.php file with this:

    <?php
    
    //Simple Redirect for WordPress.
    //Christopher Carey
    //301 Redirect for WordPress
    //http://wwww.noheat.com
    
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    
    exit();
    
    ?>

    Nothing is working :/
    Does anyone know of something that WILL work for both those versions, or at least one of them?
    Thanks!!

  2. stvwlf
    Member
    Posted 2 years ago #

  3. njras5
    Member
    Posted 2 years ago #

    It says that's a pluggable function, found in wp-includes/pluggable.php
    I guess I'm not really getting this...do I add the following code somewhere in that file (replacing, obviously, example.com with the domain home page),
    <?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

    and if so, does it matter where, and how do I ensure ONLY the error page redirects to the home page?

  4. stvwlf
    Member
    Posted 2 years ago #

    You don't change wp-includes/pluggable.php (or any other core WP file) EVER.

    I would put
    <?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
    at the top of your theme's 404.php file.

    Of course, you are changing http://www.example.com to the URL of your home page.

    and if so, does it matter where, and how do I ensure ONLY the error page redirects to the home page?

    You would have to remove all other links to the homepage from anywhere in your WP theme where they exist. Probably one in the Menu -
    go to Dashboard Nav / Appearance / Menus

    and anywhere else in the theme that links to Home.

  5. njras5
    Member
    Posted 2 years ago #

    You would have to remove all other links to the homepage from anywhere in your WP theme where they exist. Probably one in the Menu -
    go to Dashboard Nav / Appearance / Menus

    and anywhere else in the theme that links to Home

    Well NOW I feel stupid, lol. Of course that's not what I meant, but I can see where it could be taken like that. And in the 404.php file, ok, I guess that would ensure the error page redirects to the home page. (As opposed to the error page, the about page, the contact page, etc. - without clicking on the home page link. HOWEVER, I think I'm being confusing now. But anyway, I tried as you suggested, but it's not working :/ I still get the error page.

    I asked my host, they said this can not be done. I don't think this is accurate information, but I could be mistaken. CAN it be done in WP 3.3.1 or 3.3.2?

  6. stvwlf
    Member
    Posted 2 years ago #

    Please post the contents of your .htaccess file - you can change any domains in it to example.com

  7. njras5
    Member
    Posted 2 years ago #

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

    That's all there is in it.

  9. stvwlf
    Member
    Posted 2 years ago #

    Is that the .htaccess file that is in the folder /example/ or the one at the top level of your web hosting account?

    If that .htaccess is from the folder /example/ then please post what is in the .htaccess file found at the web root top level folder.

    I just did the very thing you are trying to do more or less, the other day, adding code to the top of a theme's 404.php file to redirect to home page under certain conditions. That code is working correctly so it is certainly possible to do it.

  10. njras5
    Member
    Posted 2 years ago #

    That one was in the folder /example/

    Here is the one in the root folder

    [110 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    And I *thought* it could be done somehow, that just didn't sound right, that it *couldn't* be done. I just wish I knew how :/

  11. stvwlf
    Member
    Posted 2 years ago #

    Hi

    You have another WordPress install in your root folder. It is possible the top level installation is intercepting 404's intended for the installation you are having a problem with.

    To test, add some code to the 404.php above the wp_redirect line like this:

    echo 'Now in 404.php';

    Try to visit a URL that doesn't exist and then do a browser View Source. You should find that text "now in 404.php" somewhere in the View Source code. If you don't see it there it means the 404.php file is not being entered for some reason. If that is the case you have to determine why not. Something would be handling the 404 before it reaches your theme's 404.php file.

    What exactly IS happening when you enter a URL that doesn't exist? What are you seeing? What kind of error?

  12. njras5
    Member
    Posted 2 years ago #

    Well that's bizarre. It's working now :)
    Now if I can find a custom "Not Found" page (or whatever page that is) when someone does a search I think I'll be all set.

    I don't know what happened, maybe it was a cache issue, but it's working now. Thank you so much for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic