English 中文(简体)
PHP URL 从输出缓存重写
原标题:PHP URL Rewriting from Output Buffer

我试图使用输出缓冲来重写 PHP 中的 URL 。 最理想的情况是这样。 在源代码中, 我可以用 < code> example.com/ index. php? action=123; sa=456; sa2=789 来重写 PHP 中的 URL, 但是当向浏览器输出时, 它会重新写为 < code> example.com/ 123/456//789 。 它似乎有用, 但是当它进入“ sa2” 重写输出缓冲时, 时会偶尔断开, 浏览器看到的是一个空白的白页面。 当页面加载时, 它会用 gzip 启动输出缓冲, 然后用重回调来输出缓冲。 这是我的php/ hht 访问。

<?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.......
?>

. 处理重写的通道

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 输出缓冲问题, 我似乎找不到我做错了什么。 任何帮助都会非常感激 。

最佳回答

处理此代码片段, 因为基于您使用的 php 版本, 您使用发送到重写函数的缓冲字符串, 只能包含整个缓冲的块( 打破正则表达式逻辑) 。 您必须强制将 chuck_ size 可选参数为零( 在 php 版本 5. 4 中, 并降低默认的 chuck 大小为 4096 位元) 。

查看关于取消启动的文档 :

http://es.php.net/manual/en/conference.ob-start.php" rel=“nofollow>http://es.php.net/manual/en/conference.ob-start.php

这可能是为什么这个代码"偶尔破解"的原因

问题回答

暂无回答




相关问题
URL rewrite in IIS6 - Third party tools

I have collected the following api/sdk/whatever which provide modules for doing url rewriting in IIS 5/6/7. Some are free-open source and some require $. I haven t tried any of them. I would like to ...

Rewrite spring-security redirect URLs

I m trying to get Tuckey UrlRewriteFilter to tidy up URLs for my webapp. One problem I ve got is that when spring-security notices that an anonymous user is trying to access a protected resource it ...

ASP.NET Friendly URLs

In my research, I found 2 ways to do them. Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine ...

Could I get by HttpModule the URL that contains * before?

My web-site is working under ASP.NET 3.5, IIS7, Integrated pipiline mode. My HttpModule is getting request to all resources, including URLs like this: http://my-site.com/address And I would support *...

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

URL Mod-Rewrite

I currently have a working URL: http://example.com/security-services.php?service=fixed-camera-surveillance and then I have PHP say, $_REQUEST[ service ] to do some stuff... But I d like to ...

热门标签