301 PHP redirect error - from variable
I am writing a 301 redirect, but getting an error when I pass the url through a variable
I use this script to get the current page URL and Domain name.
<?php
$mainurl = "http://" .$_SERVER["SERVER_NAME"];
$pag开发者_JAVA百科ename1 = "".$_SERVER["QUERY_STRING"];
?>
If the page does not redirect I use this to redirect the page.
<?php if ($row_rs_page['g_page_url'] != $pagename1)
$pagenameexists = "<p>page exists</p>";
?>
<?php Header( "HTTP/1.1 301 Moved Permanently" );
Header("Location: ".$mainurl."");
?>
This is where I suspect the problem occurs: Header("Location: ".$mainurl."");
If I type in http://www.example.com in the place of $mainurl the page redirects as it should. If I use the dynamic variable it throughs an error.
What am I doing wrong?
Thanks
<?php
$mainurl = "http://" .$_SERVER["SERVER_NAME"];
$pagename1 = "".$_SERVER["QUERY_STRING"];
if ($row_rs_page['g_page_url'] != $pagename1)
$pagenameexists = "<p>page exists</p>";
Header( "HTTP/1.1 301 Moved Permanently" );
Header("Location: ".$mainurl."");
?>
Shakti is right !
Do not echo anything before the LOCATION header
精彩评论