最快的加载:使用 PHP 中的标头位置重定向,还是使用 Apache 重写?

最快的加载:使用 PHP 中的标头位置重定向,还是使用 Apache 重写?

在意识到主页重定向导致我的网站出现严重延迟后,我阅读了大量优化帖子,证明 301 重定向会占用大约 100 毫秒到惊人的 400 毫秒的总加载时间。在我的网站上,这个时间大约是 250 毫秒。我知道这很愚蠢!另一方面,我真的需要重定向到默认语言,因为其他域名适用于不同的语言。我的方法有效,但每次都浪费 250 毫秒。

有什么方法可以更快地完成此操作吗?
也许可以通过 htaccess 重写?

目前有索引.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
?>

我努力了
只需将home其包含在第一个首页上即可,浏览器中的 URL 不是这样设置的,site.org/en/home您只会看到,site.org然后所有链接都不再起作用。我需要的是主页加载,浏览器中的 URL 变成/xx/home

非常感谢任何线索 +1

答案1

无需亲自检查,我猜使用 mod_rewrite 会看到性能提升,因为这样 Apache 就不必调用 PHP,而 PHP 又不必解析然后执行您的代码。

你可能想要的是这样的:-

# 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]

这是我突然想到的,所以可能不完全正确,但希望它能让你很好地了解你可以做什么。mod_rewrite 的文档非常好,它位于http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

答案2

另一种方法(从 Web 服务器角度来看可能是最快的)是使用带有RedirectPermament指令的Apache VirtualHosts

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>

相关内容