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.
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'
.
Good first right + p.
String concatenation works in php with.
<?php
$location = bloginfo('url');
header( 'Location: ' . $location ) ;
?>
try get_bloginfo () instead of bloginfo () and replace + with .
Use this:
<?php
$location = bloginfo('url');
header( 'Location:' . $location ) ;
?>
You have used Javascript syntax for concatenation in PHP
<?php
$location = bloginfo('url');
header( 'Location:'.$location ) ;
?>
This should be your syntax.
Use dot (.)
not a plus (+)
.
header( 'Location:' . $location ) ;