为什么我的301重定向需要这么长时间? [英] Why is my 301 Redirect taking so long?

查看:171
本文介绍了为什么我的301重定向需要这么长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 PHP标题位置301 处理所有主页重定向:永久重定向:

通过长时间的累赘加速我的网站,我发现重定向出了问题: website.com >> website.com/en/home website.de >> website.de/de/home etcettera etcettera (这个多语种网站大约20个),它需要200毫秒到6000毫秒的时间来完成重定向。看看这个瀑布!



之后,页面加载一个雷电眨眼!
你不会说什么浪费时间?这次服务器在做什么?
仔细检查后,我的最好的猜测是:它正在洗衣



我几乎放弃了PHP!
任何和所有令人困惑的线索都非常受欢迎+1

A。鉴于事实:Apache / 2.0.54 Fedora,PHP 5.2.9。没有数据库:只需用php包含大约15个php文件即可完成带有页眉和页脚的页面)。 YSlow等级:92/100!良好的页面速度:93/100! javascript和css尽可能地结合在一起。高速缓存控制器似乎也设置得很好(经过等级证明)。 100分中有7分缺失:没有使用Keep-Alive(beyong my controll在共享主机中,不使用内容分发网络)。我可以忍受那些缺少7分,但这是速度上的重大突破!



另外:我最近在这里得到了很好的见解,我应该使用通过htacces进行url重写。Point采取了,但是,也许sometin else 错误在这里,我应该纠正,然后再继续为我提供更困难的apache正则表达式语法。



更快的方法:当我php include code>预期的主页,而不是重定向,然后所有加载速度都很快,但url不会被重写:它位于浏览器栏上的website.com上,而我希望在将它包括在网站中后,才能成为网站。 home。对PHP来说这是可能的吗?要包含+更改url的当前地址吗?





结论:您可以使用index.php或使用.htaccess进行重定向。从我的测试沙发(来自天才答案下面!感谢每个人!)后者似乎无与伦比的速度:更快的重定向比PHP重定向!减少重定向到比第一个dns查找短。



请参阅此处如何为多语言网站执行此操作

解决方案

该死的,我讨厌陷入这种问题。您需要消除一些变量。首先,我应该指出,在开始输出内容之前,PHP不会冲洗它自己的所有标题(或者,如果output_buffering(? )ini指令被设置为x个字节,直到您输出x个字节)。所以下面的脚本直到最后才会完成发送标题:

 <?php 
header( 'Content-Type:text / pants');
睡眠(6);
头('丁丁:把袜子放在烘干机里的时间');
echoz; //标题在这里发送

如果您将 exit; echowheeeee;退出; 在该PHP脚本的最顶部?那么当你用简单的空文件替换它时会发生什么?如果带有 exit 的php脚本很慢,但纯文本文件很快,PHP解释器可能正在玩有趣的bugger。如果你仍然得到延迟,你已经消除了实际的反应产生的原因(但如果是这种情况,我仍然试图提出一些想法)。



另外,你可以SSH服务器?如果是这样,你可以尝试从服务器内部获取同一页面吗?如果你没有速度问题,我会考虑客户端。如果你不能SSH,你可以尝试从PHP请求,但我真的不知道这是否会工作:

 <$ c $ (
'http'=> array(
//发送请求标题,如果你需要
'header'=' >数组(
'Foo:Bar',
'Bar:Baz',
),
),
));
$ start = microtime(true);
$ response = file_get_contents('http://yourserver.com/',null,context);
$ end = microtime(true) - $ start;
var_dump($ end);

//出于一些奇怪的原因,PHP将这个变量放到本地范围中。
var_dump($ http_response_header);

您是否尝试过从其他机器或世界其他地方执行相同的请求?这可以确认或否认,如果它只是你的机器。



如果是响应生成,你可以尝试的另一件事是对生产进行一些黑客分析服务器。我讨厌这样做,但有时候你的代码拒绝在生产服务器上表现出来,就像它在你的开发环境或分期中表现的那样。对生成 / en / home 的脚本执行此操作:

 < ;?php 
//把它放在顶部
$ rqid = uniqid('',true);
$ h = fopen(__ DIR __。'/ crap.log','a');
fwrite($ h,$ rqid。'[START]'.microtime(true).PHP_EOL);
fclose($ h);

//做所有其他美妙的事情,比如洗衣服或者做一杯茶

//把它放在最后
$ h = fopen( __DIR __。'/ crap.log','a');
fwrite($ h,$ rqid。'[END]'.microtime(true).PHP_EOL.PHP_EOL);
fclose($ h);

针对它运行一些请求,检查以确保'crap.log'正在写入它(检查权限!!),然后你将得到一些数据,这些数据将显示脚本中是否有某些内容需要进一步调查,作为缓慢的原因。



哦,我提到了MySQL索引吗?您在请求期间是否正在进行任何查询?您是否已将所有适当的索引添加到表格中?



Steven Xu在您的问题的评论中提出了一个很好的观点 - 您确定您正在使用的程序生成瀑布给你很好的信息?如果您尚未安装 Firebug ,请点击Firefox右下方的小萤火虫图标,并确保 Net面板打开,然后重新运行您的请求并查看瀑布是否与您在所用程序中看到的结果一致。



另外,我知道这是一个骨头的建议,我很抱歉,但我认为它需要说:你的主机不允许ssh,只使用PHP 4?我会认真考虑另一个主机。它甚至可以解决这个特定的问题。



我会添加更多的东西,因为我想它。


In a long tiredsome quest to speed up my site, I have figured out something is wrong with the redirection: currently my index.php handles all the homepage redirections via PHP header location 301 Redirect Permanently: website.com >> website.com/en/home and website.de >> website.de/de/home etcettera etcettera (around 20 for this multilingual website) it takes anywhere from 200ms to 6000ms to do the redirecting. Check out the waterfall!

After that, the page loads in a thunderbolt's blink of an eye! What a waste of time wouldn't you say? What is the server doing all this time? After careful examination, my best guesse is: ITS DOING LAUNDRY!

I am almost giving up on PHP for this! Any and all clues to my puzzling prob are very welcomed +1

A. Given facts: Apache/2.0.54 Fedora, PHP 5.2.9. there is no database: just flat php files with around 15 php includes that completes my page with headers and footers). YSlow Grade: 92/100! Good page Speed: 93/100! javascript and css are as much as possible combined. Cache controlls seem well set too (as proven by the grades). Whats missing in those 7 points out of 100: not using Keep-Alive (beyong my controll in shared hosting and not using Content Delivery Network. I can live with those missing 7 points, but this is major hit on speed!

B. Furthermore: i recently was given great insights over here that i should use url rewriting via htacces. Point taken, BUT, perhaps there is sometin else wrong here that i should correct before moving on to the for me more difficult apache regex syntaxes.

C. Faster way: When I php include the intended homepage, instead of redirect, then all loads fast, but the url is not rewritten: it sits at website.com on the browser bar, whereas i wish after including it to become website.com/en/home. Is this possible with PHP? To include+change the current address of the url, too?

Conclusions: you can redirect using index.php, or using .htaccess. Sofar from my tests (coming from the genius answers below!THANKS EVERYONE!) the latter seems unmatched in speed: much faster redirecting than a php redirect! reducing the redirect to shorter than the first dns lookup.

see here how to do this correclty for multilingual site

解决方案

Damn, I hate getting stuck with this kind of problem. You need to eliminate some variables.

First I should point out that PHP will not flush all of its own headers until you start outputting things (or, if the output_buffering(?) ini directive is set to x bytes, until you have output x bytes). So the following script will not finish "sending headers" until the very end:

<?php
header('Content-Type: text/pants');
sleep(6);
header('Ding-Ding: time to put the socks in the dryer');
echo "z"; // headers are sent here

What happens to the call to en/home if you put exit; or echo "wheeeee"; exit; at the very top of that PHP script? Then what happens when you substitute it with a plain, empty file? If the php script with exit is slow but the plain text file is fast, the PHP interpreter is probably playing funny buggers. If you still get the delay for both, you've eliminated the actual response generation as the cause (but I'm still trying to come up with some ideas if this is the case).

Also, can you ssh to the server? If so, can you try wgetting the same page from inside the server? If you can without the speed problem, I would be looking at the client side. If you can't SSH, you could try doing a request from PHP, though I'm really not sure if this will work:

<?php
$context = stream_context_create(array(
    'http'=>array(
        // send request headers if you need to
        'header'=>array(
            'Foo: Bar',
            'Bar: Baz',
        ),
    ),
));
$start = microtime(true);
$response = file_get_contents('http://yourserver.com/', null, context);
$end = microtime(true) - $start;
var_dump($end);

// for some bizarre reason, PHP emits this variable into the local scope.
var_dump($http_response_header);

Have you tried doing the same request from other machines, or other places in the world? This can confirm or deny if it's just your machine.

Another thing you can try if it is the response generation is to do a little bit of hack-profiling on the production server. I hate having to do this stuff, but sometimes your code just refuses to behave on the production server like it behaves in your development environment or on staging. Do this to the script that generates /en/home:

<?php
// put this at the very top
$rqid = uniqid('', true);
$h = fopen(__DIR__.'/crap.log', 'a');
fwrite($h, $rqid.' [START] '.microtime(true).PHP_EOL);
fclose($h);

// do all that other wonderful stuff, like laundry or making a cup of tea

// put this at the very end
$h = fopen(__DIR__.'/crap.log', 'a');
fwrite($h, $rqid.' [END]   '.microtime(true).PHP_EOL.PHP_EOL);
fclose($h);

Run a few requests against it, check to make sure 'crap.log' is getting stuff written to it (check permissions!!), and then you'll have some data that will show whether there is something in your script that needs to be investigated further as the cause of the slowness.

Oh, did I mention MySQL indexes? Are you doing any queries during the request? Have you added all of the proper indexes to the tables?

Steven Xu raises a good point in the comments for your question - are you sure the program you're using to generate the waterfall is giving you good info? Try installing Firebug if you haven't already, click the little firebug icon in the bottom right of firefox and make sure the "Net" panel is open, then re-run your request and see if the waterfall is consistent with the results you're seeing in the program you used.

Also, I know this is kind of a boneheaded suggestion and I apologise, but I think it needs to be said: your host doesn't allow ssh and only uses PHP 4? I would seriously consider another host. It may even solve this specific problem.

I will add more stuff as I think of it.

这篇关于为什么我的301重定向需要这么长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆