简体   繁体   中英

How to make PHP header location redirect use PHP generated URL

This is fairly simple but I cant work it out.

I'm building a wordpress site, and I want a redirect on my 404 page using PHP instead of javascript.

<?php header("Location: www.myblogurl.com"); ?>

And I use this in my wordrpess websites to get the URL of my blog...

<?php bloginfo('url'); ?>


But I can't seem to merge the two PHP scripts together, please see my poor attempt below.

<?php

    $location = bloginfo('url');

    header( 'Location:' + $location ) ;

?>

This seems to echo my website URL instead of redirecting.

Can any help me please, thanks.

bloginfo in wordpress echo's the information instead of returning it. You should use get_bloginfo .

The docs says:

This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo() .

A sample would be:

<?php
header('Location: ' . get_bloginfo('url'));
?>

You are using the + to concatenate 2 strings. This is wrong in PHP the + is a math operator for things like $result = 1 + 1 . You need to use the . operator: $text = 'hello' . 'world' $text = 'hello' . 'world' .

Well first correct + with .

String concatenation works in php with .

 <?php

$location = bloginfo('url');

header( 'Location: ' . $location ) ;

?>

尝试使用get_bloginfo()而不是bloginfo()并将+替换为

Use this:

<?php

    $location = bloginfo('url');

    header( 'Location:' . $location ) ;

?>

You were using Javascript syntax for concatenation in PHP

<?php

    $location = bloginfo('url');

    header( 'Location:'.$location ) ;

?>

This should be your syntax.

使用dot (.)而不是plus (+)

header( 'Location:' . $location ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM