简体   繁体   English

为什么我的301重定向需要这么长时间?

[英]Why is my 301 Redirect taking so long?

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. 在一个漫长的疲惫的追求加速我的网站,我已经发现重定向有问题:目前我的index.php通过PHP标题位置处理所有主页重定向301重定向永久: website.com >> website.com/en/homewebsite.de >> website.de/de/home etcettera etcettera(这个多语言网站大约20个)需要200ms到6000ms才能进行重定向。 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! 我几乎放弃了PHP! Any and all clues to my puzzling prob are very welcomed +1 任何和我所有令人费解的问题的线索都非常欢迎+1

A. Given facts: Apache/2.0.54 Fedora, PHP 5.2.9. 答:事实: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). 没有数据库:只有大约15个php的平面php文件,包括用页眉和页脚完成我的页面)。 YSlow Grade: 92/100! YSlow等级:92/100! Good page Speed: 93/100! 好页面速度:93/100! javascript and css are as much as possible combined. javascript和css尽可能合并。 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! 在100分中的7分中缺少什么:不使用Keep-Alive(在共享主机中使用我的控制权而不使用内容分发网络。我可以忍受那些缺少7分的人,但这是对速度的重大打击!

B. Furthermore: i recently was given great insights over here that i should use url rewriting via htacces. B.此外:我最近在这里得到了很好的见解,我应该通过htacces使用url重写。 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. 点了,但是,也许还有其他错误,我应该纠正,然后继续为我更难的apache正则表达式语法。

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. C.更快的方式:当我的php include预期的主页,而不是重定向,然后所有加载快,但网址不被重写:它位于浏览器栏上的website.com,而我希望包括它成为网站。 COM / EN /家。 Is this possible with PHP? 这可能用PHP吗? To include+change the current address of the url, too? 要包含+更改网址的当前地址?

截图

Conclusions: you can redirect using index.php, or using .htaccess. 结论:您可以使用index.php或使用.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! 从我的测试中得到Sofar(来自下面的天才答案!感谢所有人!)后者在速度上看起来无与伦比:重定向比重定向更快! reducing the redirect to shorter than the first dns lookup. 将重定向减少到比第一个dns查找短。

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). 首先,我应该指出,在您开始输出内容之前,PHP不会刷新所有标头(或者,如果output_buffering(?)ini指令设置为x字节,直到输出x字节为止)。 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; 如果你exit;对en / home的调用会发生什么变化exit; or echo "wheeeee"; exit; 或者echo "wheeeee"; exit; echo "wheeeee"; exit; at the very top of that PHP script? 在PHP脚本的最顶端? 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. 如果exit的php脚本很慢但是纯文本文件很快,那么PHP解释器可能正在播放有趣的bug。 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? 另外,你可以ssh到服务器? 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: 如果你不能SSH,你可以尝试从PHP做一个请求,虽然我真的不确定这是否会起作用:

<?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. 如果是响应生成,您可以尝试的另一件事是在生产服务器上进行一些hack-profiling。 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 : 对生成/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. 针对它运行一些请求,检查以确保'crap.log'正在写入内容(检查权限!!),然后您将获得一些数据,以显示脚本中是否存在需要的内容作为缓慢的原因进一步调查。

Oh, did I mention MySQL indexes? 哦,我提到了MySQL索引吗? 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? Steven Xu在你的问题评论中提出了一个很好的观点 - 你确定你用来生成瀑布的程序给你提供了很好的信息吗? 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. 尝试安装Firebug,如果你还没有,点击firefox右下角的小火炬图标,确保“Net”面板打开,然后重新运行你的请求,看看瀑布是否与你的结果一致在你使用的程序中看到。

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? 此外,我知道这是一个愚蠢的建议,我道歉,但我认为需要说:你的主机不允许ssh,只使用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. 我会想到它会添加更多东西。

If it is indeed the headers taking ages, then your JS/CSS/HTML is irrelevant. 如果它确实是标题需要很长时间,那么你的JS / CSS / HTML就无关紧要了。

You can do the forwarding in .htaccess . 您可以在.htaccess进行转发。

RewriteEngine On
RewriteRule ^$ en/home [R=301]

This will essentially send the same header, but it won't invoke the PHP engine first to do it :) 这将基本上发送相同的标头,但它不会首先调用PHP引擎:)

Update 更新

On closer inspection, it would seem to me that your en/home page is taking the longer time to download. 仔细观察,在我看来你的en/home页面需要更长的下载时间。

I think Ignacio Vazquez-Abrams may have the answer: after you call header() to do the redirection you need to call exit() to cause the PHP script execution to stop. 我认为Ignacio Vazquez-Abrams可能有答案:在调用header()进行重定向之后,需要调用exit()以使PHP脚本执行停止。 Without that the script will keep executing, sending output to the browser, until the end. 没有它,脚本将继续执行,将输出发送到浏览器,直到结束。 Since the browser has to wait for the server side script to end before performing the redirection that could cause the problem. 由于浏览器必须等待服务器端脚本结束才能执行可能导致问题的重定向。

Update 更新

Just read Alex's update and he seems to be correct. 刚刚阅读Alex的更新,他似乎是正确的。 The /en/home page is where the time is. / en / home页面是时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM