Php 如何根据用户语言提高重定向到子域的速度?
我正在优化我们的网页,我们已经注意到一些我们想要显著改进的东西。我们使用Symfony 1.3 当用户加载example.com时,将执行过滤器(渲染、安全和记忆)。然后我们执行。如果用户是第一次出现在这里,我们将获得其浏览器的首选语言,并将网页重定向到en.example.com或es.example.com。如果用户有一个会话,我们从它的会话中获取语言;我们重定向到子域。然后再次加载en.example.com页面 我们在重定向上损失了大约1.5秒。en.example.com的加载速度有时会更快。我们怎样才能摆脱这种拖延呢?更改index.php并直接执行浏览器memcache或db查询,而不加载symfonyPhp 如何根据用户语言提高重定向到子域的速度?,php,performance,redirect,symfony1,subdomain,Php,Performance,Redirect,Symfony1,Subdomain,我正在优化我们的网页,我们已经注意到一些我们想要显著改进的东西。我们使用Symfony 1.3 当用户加载example.com时,将执行过滤器(渲染、安全和记忆)。然后我们执行。如果用户是第一次出现在这里,我们将获得其浏览器的首选语言,并将网页重定向到en.example.com或es.example.com。如果用户有一个会话,我们从它的会话中获取语言;我们重定向到子域。然后再次加载en.example.com页面 我们在重定向上损失了大约1.5秒。en.example.com的加载速度有时
非常感谢 你可以像这样使用mod_重写
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^(.*)$ http://es\.example\.com/$1 [R=301,L]
对于每种支持的语言。不涉及PHP,不需要会话,只是速度很快。我终于在index.PHP上进行了重定向。从1到1.5秒到40毫秒。比如:
<?php
$host = $_SERVER['HTTP_HOST'];
$host_a = explode('.', $host);
// if subdomain is not in the supported langs
$langs = array('en', 'es');
if( !in_array($host_a[0], $langs) ){
// try to get the cookie and the user culture
$cookie = $_COOKIE['symfony'];
list($id, $signature) = explode(':', $cookie, 2);
if( $signature == sha1($id.':'.'secret_cookie') )
{
// get cookie data from memcache
$memcache_obj = memcache_connect('localhost', 11211);
// the cookie is built with two parts separated by :
// - md5 of the sfCache directory
// - $id from the user
$md5_dir = md5(dirname(dirname(dirname(__FILE__)).'/lib/vendor/symfony/lib/cache/sfCache.class.php'));
$session = memcache_get($memcache_obj, $md5_dir.':'.$id);
$user_lang = $session['symfony/user/sfUser/culture'];
if( !in_array($user_lang, $langs) ) $user_lang = 'en';
// if not, get browser lang
}else{
$user_lang = prefered_language($langs);
}
// build url
$url = 'http://'.$user_lang.'.'.str_replace('www.', '', $_SERVER['HTTP_HOST']).$_SERVER['REQUEST_URI'];
// and redirect
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".$url);
die();
}
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
sfContext::createInstance($configuration)->dispatch();
谢谢,但问题是我必须考虑用户设置才能重定向到英语或西班牙语版本。如果用户转到example.com,我必须在重定向到一个子域或另一个子域之前进行检查。这更像是一个Symfony问题。那么RewriteMap userpref prg:db_reader_daemon.php
呢?