Content Marketing

Redirect WordPress in Header

The redirection plugin built for WordPress is a fantastic means of organizing and managing redirects. I use it on this site and have organized my groups of redirects for updated posts, affiliate links, downloads, etc.

However, I ran into a unique problem where I have a reverse proxy set up for a client where WordPress is running at a path… but not the root of the site. The primary site is running on IIS in Azure. IIS can manage redirects just as any web server can, but the problem is that this client would need to put redirect management into their development process – and they’re busy already.

At issue is that a typical .htaccess style redirect isn’t a possibility… we have to actually write the redirects in PHP. As a solution, we route the requests to WordPress to identify if there are any redirects on old paths.

Within the header.php file of our child theme, we have a function:

function my_redirect ($oldlink, $newlink, $redirecttype = 301) {
	$olduri = $_SERVER['REQUEST_URI'];
	if(strpos($olduri, $oldlink) !== false) {
		$newuri = str_replace($oldlink, $newlink, $olduri);
		wp_redirect( $newuri, $redirecttype );

We didn’t bother putting the function in functions.php simply because it would only be impacting the header file. Then, within the header.php file, we simply have a list of all the redirects:

my_redirect('lesson_plans', 'lesson-plan');
my_redirect('resources/lesson-plans/26351', 'lesson-plan/tints-and-shades');
my_redirect('about/about', 'about/company/');

With that function, you can also specify what type of redirect you’d like to set the header request to, we’ve just defaulted it to a 301 redirect so that the search engines will honor it.

Douglas Karr

Douglas Karr is the founder of the Martech Zone and CEO of DK New Media. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles


Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.