我的脚本有一个奇怪的错误,如果URL以"/"结尾,我只想将用户重定向到同一个URL,而结尾没有任何"/"。
我得到这个错误:
Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/xxx/httpdocs/series.php:1) in /home/httpd/vhosts/xxx/httpdocs/series.php on line 7
Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/mxxx/httpdocs/series.php:1) in /home/httpd/vhosts/xxx/httpdocs/series.php on line 8
代码:
<?php
$urlLast = $_SERVER['REQUEST_URI'];
$urlLast = substr($urlLast, -1);
if (($urlLast == '/') && (!strstr($_SERVER['REQUEST_URI'], 'en-streaming')))
{
$newURL = substr($_SERVER['REQUEST_URI'],0,-1).'-en-streaming';
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$newURL."");
}
您可以使用进行修复
ob_start();在你的剧本顶部。
<?php
之前是否有Unicode BOM?
当您使用编码"UTF-8 with BOM(字节顺序标记)"保存文件时,特殊字符序列(0xEF0xBB0xBF)将自动插入文件的开头。
只需尝试使用"UTF-8 with BOM"(如果存在)保存文件,或者尝试其他编辑器。
notepad.exe中的AFAIK-UTF-8是"WITH BOM"。
删除<?php
之前的尾随空格
此外,删除?>
(如果有)后的空格
此外,在文件的开头添加ob_start()
。
这会将输出保存在缓冲区缓存中,而不是在浏览器上打印。