Quantcast
Channel: Martech Zone
Viewing all articles
Browse latest Browse all 1488

Redirect WordPress in Header

$
0
0

WordPress Header Redirect

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 );
exit;
}
}

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.

Download a Sponsored Marketing Whitepaper:
2019 CX Virtual Event: 5 Tactics to Increase Customer Engagement

2019 CX Virtual Event: 5 Tactics to Increase Customer Engagement

Join Argyle Executive Forum, in partnership with Bold360 by LogMeIn, to learn ways to gain actionable insights & methods to increase customer engagement. Download Now

The post Redirect WordPress in Header appeared first on Martech Zone.


Viewing all articles
Browse latest Browse all 1488

Trending Articles