我正在尝试使用输出缓冲区在PHP中重写我的URL。理想情况下,在我的源代码中我可以保持我的链接为example.com/index.php?action=123;sa=456;sa2=789
但是当输出到浏览器时它会重写为example.com/123/456/789
它似乎正在工作但是当它到达“sa2”时重写输出缓冲区偶尔会破坏,所有浏览器都看到一个空白的白页。当页面加载时,它会启动带有gzip的输出缓冲区,然后是带有重写回调的输出缓冲区。这是我的php / .htaccess
<?php
if (!ob_start("ob_gzhandler"))
ob_start();
// Handle Rewriting of URLs
RewriteURLs();
function RewriteURLs()
{
// rewrite URLS from output buffer
function rewrite($buffer)
{
// pages
$buffer = preg_replace('/index.php\?page=(.*?)/is', "page/$1", $buffer);
// sub-sub-actions
$buffer = preg_replace('/index.php\?action=(.*?);sa=(.*?);sa2=(.*?)/is', "$1/$2/$3", $buffer);
// sub-actions
$buffer = preg_replace('/index.php\?action=(.*?);sa=(.*?)/is', "$1/$2", $buffer);
// actions
$buffer = preg_replace('/index.php\?action=(.*?)/is', "$1", $buffer);
// index.php removal
$buffer = preg_replace('/index.php/is', "", $buffer);
return $buffer;
}
// start for rewriting
ob_start('rewrite');
}
// rest of content/page loading.......
?>
.htaccess来处理重写
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Rules for: pages
RewriteRule ^page/([-_!~*'()$\w]+)/?$ index.php?page=$1 [L,QSA]
# Rules for sub-sub-actions
# /{action}/{subaction}/{subsubaction}/
RewriteRule ^([-_!~*'()$\w]+)/([-_!~*'()$\w]+)/([-_!~*'()$\w]+)/?$ index.php?action=$1;sa=$2;sa2=$3
# Rules for sub-actions
# /{action}/{subaction}/
RewriteRule ^([-_!~*'()$\w]+)/([-_!~*'()$\w]+)/?$ index.php?action=$1;sa=$2
# Rules for: actions
# /{action}/
RewriteRule ^([-_!~*'()$\w]+)/?$ index.php?action=$1 [L,QSA]
这看起来越来越像PHP输出缓冲区问题我似乎无法找到我做错了什么。非常感谢任何帮助。
答案 0 :(得分:1)
处理这段代码因为基于php版本你使用发送到重写函数的缓冲区字符串只能包含整个缓冲区的一大块(打破正则表达式逻辑)。你必须强制chuck_size可选参数为零(在php版本5.4和更低的默认chuck大小是4096字节)
查看有关ob_start的文档:
http://es.php.net/manual/en/function.ob-start.php
这可能就是为什么这段代码“偶尔会破坏”