Last active
August 29, 2015 14:12
-
-
Save k4zuki02h4t4/4ff95935abc5d01bde8b to your computer and use it in GitHub Desktop.
WordPress Simple 301 Redirect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function http_host_301_redirect() { | |
$site_host = parse_url( get_bloginfo('url', 'raw' ) ); | |
$site_host = $site_host['host']; | |
$http_host = $_SERVER["HTTP_HOST"]; | |
$redirect = ( empty( $_SERVER["HTTPS"] ) ? "http://" : "https://" ) . $site_host . $_SERVER["REQUEST_URI"]; | |
if ( $site_host === $http_host || !wp_http_validate_url( $redirect ) ) | |
return; | |
wp_redirect( $redirect, 301 ); | |
} | |
add_action( 'template_redirect', 'http_host_301_redirect', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be redirected.
http://old.example.com/hoge -> http://new.example.com/hoge
Can not be redirected.
http://old.example.com/hoge -> http://new.example.com/fuga