Skip to content

Instantly share code, notes, and snippets.

@dlucero23
Last active February 12, 2019 15:03
Show Gist options
  • Save dlucero23/f65924e943c77600302ac9586f7556b1 to your computer and use it in GitHub Desktop.
Save dlucero23/f65924e943c77600302ac9586f7556b1 to your computer and use it in GitHub Desktop.
Redirect a WP page URL without a plugin or messing with domain DNS settings. Replace the [from slug] and [to slug] below the line starting with switch with the wp slugs for each URL (ie. [from slug] would be /tos , [to slug] would be /terms-of-service)
function redirect_page() {
if (isset($_SERVER['HTTPS']) &&
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
$currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$currenturl_relative = wp_make_link_relative($currenturl);
switch ($currenturl_relative) {
case '[from slug]':
$urlto = home_url('[to slug]');
break;
default:
return;
}
if ($currenturl != $urlto)
exit( wp_redirect( $urlto ) );
}
add_action( 'template_redirect', 'redirect_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment