0

after becoming aware of the major lag that hits my site becaouse of the homepage redirect, i read into numerous optimization threads, proving that a 301 redirect slams out approximately 100ms to a staggering 400ms out of the total load time. On my website its around 250ms. Silly i know! On the other hand i really do need a redirect to the default language, because other domains for the different languages. What i have works, but wastest 250ms every single time.

Is there anyway this can done be faster?
Through htaccess rewrite perhaps?

CURRENTLY HAVE index.php

<?php
switch($_SERVER["HTTP_HOST"]){
case "site.org":
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: /en/home');   # extensio .php is hidden: 'home' is a file

case "site.fr":
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: /fr/home');   # extensio .php is hidden: 'home' is a file
etc etc
?>

I HAVE TRIED
Simply including home that works for the first homepage beautifully, BUT the url in the browser is in this way NOT set to site.org/en/home you just see the site.org and all links then dont work anymore. What i need is that the homepage loads AND the url in the browser becomes /xx/home

Any and all clues are very appreciated +1

2 Answers 2

1

Without checking myself, I would guess that you'll see a performance increase by using mod_rewrite, because that way Apache isn't having to invoke PHP, which in turn isn't having to parse and then execute your code.

What you probably want is something like this:-

# Check for site.org/
RewriteCond       %{REQUEST_URI}   ^/$
RewriteCond       %{HTTP_HOST}     ^site.org$
RewriteRule       ^/$              /en/home         [RL]

# Check for site.fr/
RewriteCond       %{REQUEST_URI}   ^/$
RewriteCond       %{HTTP_HOST}     ^site.fr$
RewriteRule       ^/$              /fr/home         [RL]

This is off the top of my head, so it's probably not completely correct, but hopefully it'll give you a good idea of what you can do. The documentation for mod_rewrite is pretty good, and it's at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html.

3
  • Thanks Andy, +1, this sounds more familiar to me. i like the code layout! keeps things neat. Is this faster than Virtual host method that ms has suggested. Gave it a try but helas: errors and nothing loads.. Log file says doesnt know that flag [RL] on Apache/2.0.54 (Fedora). Any idea who to get this redirect to work?
    – Sam
    Dec 18, 2010 at 6:41
  • 1
    Sorry Sam - the [RL] should be [R,L] - in my defence it was quite late ;-)
    – Andy Smith
    Dec 18, 2010 at 11:56
  • Just noticed as well that my original idea would redirect site.org to site.org/en/home, and site.fr to site.fr/fr/home. If you change the second field of the RewriteRule lines to site.org/en/home and site.org/fr/home, it'll be a bit neater. Speedwise it should be the same as ms's suggestion - they're both using mod_rewrite so it's being handled within Apache itself.
    – Andy Smith
    Dec 18, 2010 at 11:59
3

Another (probably the fastest from web server perspective) way is to use Apache VirtualHosts with RedirectPermament directive

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.site.org
ServerAlias site.org *.site.org
<LocationMatch "^/$">
RedirectPermanent /en/home
</LocationMatch>
</VirtualHost>

<VirtualHost *:80>
ServerName www.site.fr
ServerAlias site.fr *.site.fr
<LocationMatch "^/$">
RedirectPermanent /fr/home
</LocationMatch>
</VirtualHost>
2
  • Thanks MS, +1, sounds too good to be true, on shared hosting with plesk 8.2 and usual privileges? Will give it a try now.
    – Sam
    Dec 18, 2010 at 5:58
  • You canot use Virtual Hosts in .htaccess. It should be placed in server config context. In htaccess you could use mod_rewrite, as Andy suggested.
    – sumar
    Dec 19, 2010 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .