Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 341
Sam Avatar asked Dec 19 '10 00:12

Sam


People also ask

How long do 301 redirects take to work?

Google's Webmaster Trends Analyst John Mueller said that 301 redirects applied to permanent site move situations should be active for a long time. He explained that it takes at least six months to roughly a year for Google to be able to completely recognize that your site was moved.

How long does it take for a redirect to take effect?

If everything is configured properly, it takes about 30 minutes to start working. Read more about different types of redirects.

Do 301 redirects work instantly?

A 301 redirect ensures that when a page is permanently moved, the website visitors and the search engines are sent automatically to the corresponding new page. Or, they get intelligently redirected to the right category page; or to the home page. 301 redirects are handled instantly by the server based on a file named .


1 Answers

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.

like image 167
Shabbyrobe Avatar answered Sep 21 '22 12:09

Shabbyrobe