luc*_*ina 5 cakephp cakephp-2.0
在我的网站上,用户可以通过公共配置文件访问http://mysite.com/vanity_url.我想允许用户将自己的域指向我网站上的个人资料页面.就像Bandcamp一样.
我的Profile模型有两个字段来处理:vanity_url这是普通的用户名类型的字段; 和一个新的custom_domain,这是他们自己的域名,例如,example.com.
这是我到目前为止所做的,但我担心它可能不是最优雅,最安全,最有效的方法.
首先,我确保将Apache DocumentRoot设置为我的应用程序webroot目录,这样我就可以告诉用户将DNS指向我站点的IP.
现在,这就是我的routes.php外观上的路由规则:
if (preg_match('/mysite\.com\.?$/', $_SERVER['SERVER_NAME'])){
// Normal routes when visitors go to my domain
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/**', array('controller' => 'pages', 'action' => 'display'));
// Move all other actions to a separate '/app/' area
Router::connect('/app/:controller/:action/**');
Router::connect('/app/:controller/**');
// Handle profile URLs
Router::connect('/:profile/**',
array('controller' => 'profiles', 'action' => 'view'),
array('pass' => array('profile'), 'profile' => '[0-9a-zA-Z\-\_]+')
);
}
else{
// If visitors come via a URL different to mysite.com, I let
// the ProfilesController deal with it passing the current SERVER_NAME
// as a param to the 'view' action
Router::connect('/', array(
'controller' => 'profiles',
'action' => 'view',
$_SERVER['SERVER_NAME'], // 'url' param
true // 'customDomain' param
));
Router::redirect('/*', 'http://mysite.com');
}
Run Code Online (Sandbox Code Playgroud)
这就是view动作ProfilesController看起来像:
public function view($url = null, $customDomain = false) {
if ($url){
// Find the profile by its vanity_url or its custom_domain
$findOptions = array(
'conditions' => $customDomain?
array('custom_domain' => $url) :
array('vanity_url' => $url)
);
if ($profile = $this->Profile->find('first', $findOptions)) {
$this->set('profile', $profile);
}
}
else throw new NotFoundException(__('Invalid profile'));
}
Run Code Online (Sandbox Code Playgroud)
这种方法可以解决哪些问题?
此外,有谁知道为什么Bandcamp要求用户创建CNAME而不是A记录来设置子域?我错过了我应该考虑的事情吗?
编辑有人帮助我弄清楚最后一点:似乎你不能轻易地使用CNAME记录将裸域指向另一个.主要问题仍然是开放的.