blackhatx2

How to EASILY 301 redirect all 404 error pages

Sep 17th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. https://blackhatx.com/seo-forum/
  2.  
  3.  
  4. Alright so a few days ago I successfully purchased an expiring domain from an auction site, and after wasting a few hours doing 301 redirects manually (monitoring 404s and adding redirects by hand in .htaccess file) I accidently found this super easy method to 301 redirect all 404 error pages to root domain.
  5.  
  6. This trick was simply done by a WP core function called wp_redirect(). Here are the parameters for the function:
  7. Code:
  8. $location
  9. (string) (required) The absolute URI which the user will be redirected to.
  10. Default: None
  11.  
  12. $status
  13. (integer) (optional) The status code to use.
  14. Default: 302
  15.  
  16. Here's how the function should be used:
  17. Code:
  18. <?php
  19. wp_redirect( $location, $status );
  20. exit;
  21. ?>
  22.  
  23. Here's a real life example
  24. Code:
  25. <?php wp_redirect( get_option( 'siteurl' ); exit; ?>
  26.  
  27. What this does is, calling the get_option('siteurl') will basically return the default domain name that you're using (One that you've set in the Settings > General tab). And then, redirects it using a 302 since we haven't exclusively specified that we need a 301 redirect (302 is the default).
  28.  
  29. But.. this isn't what we want, we need it to redirect with a 301, right?
  30. Code:
  31. <?php wp_redirect(get_option('siteurl'),301); ?>
  32.  
  33. Yep, right there. This basically gets the default domain name and makes the redirect with a 301.
  34.  
  35. So how to redirect all error pages to default domain name (root domain) with a 301?
  36.  
  37. Simple, create a file and name it 404.php and paste the above code in it, then replace your existing 404.php with it.
  38.  
  39. Remember that you need to replace the 404.php in your theme folder, which is located at wp-content > themes > your theme.
  40.  
  41. For more information visit are site...
  42. https://blackhatx.com/seo-forum/
Advertisement
Add Comment
Please, Sign In to add comment