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

发布于 2024-10-08 11:02:20 字数 1382 浏览 7 评论 0原文

在漫长而疲惫的加速我的网站速度的探索中,我发现重定向出了问题:目前我的index.php通过PHP标头位置301永久重定向处理所有主页重定向: website.com >>> website.com/en/homewebsite.de >> website.de/de/home 等等(这个多语言网站大约有 20 个)执行重定向需要 200 毫秒到 6000 毫秒不等。看看瀑布!

之后,页面加载眨眼间就完成了! 你不说这是浪费时间吗?服务器这段时间在做什么? 经过仔细检查,我最好的猜测是:它在洗衣服

为此我几乎要放弃 PHP 了! 任何和所有关于我令人费解的问题的线索都非常受欢迎+1

A。鉴于事实:Apache/2.0.54 Fedora,PHP 5.2.9。没有数据库:只有大约 15 个 php 的平面 php 文件,这些文件通过页眉和页脚完成了我的页面)。 Y慢等级:92/100!良好的页面速度:93/100! javascript和css尽可能的结合起来。缓存控制似乎也设置得很好(正如成绩所证明的那样)。 100 分中的 7 点缺少什么:不使用 Keep-Alive(超出我对共享托管的控制并且不使用内容分发网络。我可以忍受缺少 7 点,但这对速度造成重大影响!

B. 此外:我最近在这里得到了很好的见解,我应该通过 htacces 进行 url 重写,但是,也许这里还有一些错误,我应该在转向对我来说更困难的 apache 之前纠正。 C. 更快的

方法:当我 php include 预期的主页而不是重定向时,所有内容都会快速加载,但 url 不会被重写:它位于浏览器栏上的 website.com,而我希望将其包含为 website.com/en/home。这也可以使用 PHP 来包含+更改 url 的当前地址吗

? 2cKmb.png" rel="nofollow noreferrer">screenshot

结论:您可以使用index.php进行重定向,或使用.htaccess。到目前为止,从我的测试来看(来自下面的天才答案!谢谢大家!)后者的速度似乎无与伦比:重定向比 php 重定向快得多!将重定向减少到比第一个 DNS 查找更短。

在此处查看如何操作多语言网站的正确性

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?

screenshot

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-10-15 11:02:20

该死,我讨厌陷入这种问题。你需要消除一些变量。

首先我应该指出,在您开始输出内容之前,PHP 不会刷新所有自己的标头(或者,如果 output_buffering(?) ini 指令设置为 x 字节,直到您输出 x 字节)。因此,以下脚本直到最后才会完成“发送标头”:

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

如果您输入 exit;echo "wheeeee"; ,对 en/home 的调用会发生什么? exit; 在 PHP 脚本的最顶部?那么当你用一个普通的空文件替换它时会发生什么?如果带有 exit 的 php 脚本很慢,但纯文本文件很快,那么 PHP 解释器可能正在玩有趣的游戏。如果两者仍然出现延迟,则说明您已经排除了实际响应生成的原因(但如果是这种情况,我仍在尝试提出一些想法)。

另外,你可以ssh到服务器吗?如果是这样,您可以尝试从服务器内部获取相同的页面吗?如果可以没有速度问题,我会考虑客户端。如果你不能 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);

你是否尝试过从其他机器或世界上的其他地方发出相同的请求?这可以确认或否认这是否只是您的机器。

如果是响应生成,您可以尝试的另一件事是在生产服务器上进行一些黑客分析。我讨厌必须做这些事情,但有时您的代码只是拒绝在生产服务器上表现得像在开发环境或登台时一样。对生成 /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);

对其运行一些请求,检查以确保“crap.log”正在写入内容(检查权限!!),然后您将获得一些数据,这些数据将显示脚本中是否有某些内容需要进一步调查导致速度缓慢的原因。

哦,我有提到 MySQL 索引吗?您在请求期间是否有任何疑问?您是否已将所有正确的索引添加到表中?

史蒂文·徐(Steven Xu)在您的问题的评论中提出了一个很好的观点 - 您确定您用来生成瀑布的程序为您提供了好的信息吗?如果您还没有安装 Firebug,请尝试安装,请单击 Firefox 右下角的小 Firebug 图标,并确保“ Net”面板打开,然后重新运行您的请求并查看瀑布是否与您在使用的程序中看到的结果一致。

另外,我知道这是一个愚蠢的建议,我对此表示歉意,但我认为有必要说一下:您的主机不允许 ssh 并且只使用 PHP 4?我会认真考虑另一位房东。它甚至可以解决这个特定问题。

当我想到更多的东西时,我会添加更多内容。

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.

只是我以为 2024-10-15 11:02:20

如果确实是标题花了很长时间,那么你的 JS/CSS/HTML 就无关紧要了。

您可以在.htaccess中进行转发。

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

这本质上会发送相同的标头,但它不会首先调用 PHP 引擎来执行此操作:)

更新

经过仔细检查,在我看来,您的 en/home 页面花费了更长的时间是时候下载了。

If it is indeed the headers taking ages, then your JS/CSS/HTML is irrelevant.

You can do the forwarding in .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 :)

Update

On closer inspection, it would seem to me that your en/home page is taking the longer time to download.

深空失忆 2024-10-15 11:02:20

我认为 Ignacio Vazquez-Abrams 可能有答案:在调用 header() 进行重定向后,您需要调用 exit() 来使 PHP 脚本执行停止。如果没有的话,脚本将继续执行,将输出发送到浏览器,直到结束。由于浏览器必须等待服务器端脚本结束才能执行可能导致问题的重定向。

更新

刚刚阅读了 Alex 的更新,他似乎是正确的。 /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. 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. The /en/home page is where the time is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文