Conditional Redirect In Wordpress
Posted on March 9, 2025
function redirect_plain_cms_to_frontend() {
if ( isset( $_SERVER['HTTP_HOST'] ) &&
$_SERVER['HTTP_HOST'] === 'cms.example.ee' || 'stage.cms.example.ee') {
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/';
if ( ( $request_uri === '/' || $request_uri === '/index.php' ) ) {
wp_redirect( 'https://example.ee', 301 );
exit();
}
}
}
add_action( 'template_redirect', 'redirect_plain_cms_to_frontend' );
Hey! I'll happily receive your comments
via email.
Thanks for reading.

Andreas Wagner
freelance System Administrator and Ruby programmer in Tallinn, Estonia