PHP 301 Redirection in WordPress

WordPress 301 Redirection

There are many ways to accomplish 301 redirection in WordPress. In this blog post we’ll show you the 2 most common options when you want to send 301 Moved Permanently header response codes.

  1. Include PHP Redirection Code in header.php
  2. Create a redirect rule in functions.php using built in wp_redirect function

Note: whenever you use 301 using PHP its wise to exit the call using exit function as shown in the sample codes.

How to 301 Redirect Using PHP in WordPress Using header.php

Below Example Shows Just PHP 301 Response Codes

PHP Permanent 301 Redirection HTTP/2 Protocol <?php header("HTTP/2 301 Moved Permanently"); header("Location: https://www.example.com/"); exit(); ?> PHP Permanent 301 Redirection Shorthand format <?php header("Location: https://www.example.com/", true, 301); exit(); ?> Above examples can be used on any content management system.

But we want to focus on WordPress built sites, and as detailed in the video lesson above, you can use WordPress Conditional Tags and PHP together for 301 Redirections.

PHP Example Codes for WordPress

<?php if (is_page('contact') ) { header("HTTP/2 301 Moved Permanently"); header("Location: https://www.example.com/newContact"); exit(); } ?> WordPress has plenty of other built in conditional tags which assists website developers with different requirements. For example: in_category function is ideal when you want to redirect OLD category URLs to New Category URLs <?php if (in_category('sampleCategoryName') ) { header("HTTP/2 301 Moved Permanently"); header("Location: https://www.example.com/newCategoryURL"); exit(); } ?>

WordPress Built in wp_redirect() Function for 301 Redirection

Not only WordPress allows website owners to use normal PHP header response option, WordPress also has built in redirection function called wp_redirect() that does the same thing.

Example below is ideal when moving site to new domain <?php wp_redirect( 'https://www.example.com/', 301 ); exit; ?> You can use built in WordPress wp_redirect() function to 301 a specific PAGE <?php if (is_page('oldPageURL') ) { wp_redirect( 'https://www.example.com/newPageURL', 301 ); exit; } ?> You can use PHP OR operator to redirect multiple PAGES <?php if (is_page('oldPageURL') || is_page('oldPageURLTwo') ) { wp_redirect( 'https://www.example.com/newPageURL', 301 ); exit; } ?> You can use built in wp_redirect function to 301 specific POST <?php if (is_single('oldBlogPostURL') ) { wp_redirect( 'https://www.example.com/newBlogPostURL', 301 ); exit; } ?> You can use PHP OR operator to redirect multiple blog POSTS <?php if (is_single('oldBlogPostURL') || is_single('oldBlogPostURLTwo') ) { wp_redirect( 'https://www.example.com/newBlogPostURL', 301 ); exit; } ?>

How to 301 Redirect Using PHP in WordPress Using functions.php

Location of functions.php file usually is Web Hosting > File Manager > public_html > wp-themes > theme > yourCurrentTheme > functions.php where you can add 301 using template_redirect action add_action( 'template_redirect', 'rankya_redirection_rules' ); function rankya_redirection_rules() { if ( is_page( 'oldPageURL' ) ) { wp_redirect( 'https://www.example.com/newPageURL', 301 ); exit; } } PHP else if example add_action( 'template_redirect', 'rankya_redirection_rules' ); function rankya_redirection_rules() { if ( is_page( 'oldPageURL' ) ) { wp_redirect( 'https://www.example.com/newPageURL', 301 ); exit; } elseif ( is_single( 'oldBlogPostURL' ) ) { wp_redirect( 'https://www.example.com/newBlogPostURL', 301 ); exit; } } You can redirect an entire category add_action( 'template_redirect', 'rankya_redirection_rules' ); function rankya_redirection_rules() { if ( in_category( 'sampleCategoryName' ) ) { wp_redirect( 'https://www.example.com/newCategoryURL', 301 ); exit; } } Or you can even use good old PHP version add_action( 'template_redirect', 'rankya_redirection_rules' ); function rankya_redirection_rules() { if ( is_page( 'oldPageURL' ) ) { header("HTTP/2 301 Moved Permanently"); header("Location: https://www.example.com/URL"); exit(); } }

By RankYa

RankYa is a digital services provider dedicated to growing your sales and business website's results. Highly experienced technical problem solver, Google products expert with proven 'Social Media Marketing' skills, RankYa (100% Australian Owned and Operated) is dedicated to helping small businesses to grow.

We're looking forward to contributing towards your online success. Contact Us.

1 comment

Questions? Leave a Comment!

Your email address will not be published. Required fields are marked *