How to redirect PHP header header location using PHP generated url
It's pretty simple, but I can't seem to solve it.
I am creating a wordpress site and I want to redirect to my 404 page using PHP instead of javascript.
<?php header("Location: www.myblogurl.com"); ?>
And I use this on my wordrpess sites to get my blog url ...
<?php bloginfo('url'); ?>
But I cannot combine the two PHP scripts, please see my failed attempt below.
<?php
$location = bloginfo('url');
header( 'Location:' + $location ) ;
?>
This seems to echo my website url instead of redirecting.
Can anyone help me please, thanks.
source to share
bloginfo
in wordpress echo information instead of returning it. You must use get_bloginfo
.
The docs say:
This always prints the result to the browser. If you need values to use in PHP, use
get_bloginfo()
.
The sample will look like this:
<?php
header('Location: ' . get_bloginfo('url'));
?>
You are using +
2 strings to concatenate. This is not true in PHP, it +
is a mathematical operator for things like $result = 1 + 1
. You need to use the .
: operator $text = 'hello' . 'world'
.
source to share