Pergunta

I'd like to ask for your help how to setup routing properly for a Cakephp web application on AWS. I got Cakephp running on AWS Elastic Beanstalk, using PHP configuration, and then I set an A record for my domain (e.g. foo.com) linking to Cloudfront CDN that was linked to the default ELB of Beanstalk. Everything was working fine, until I found out all redirections handled by Cakephp will redirect to the default public DNS of ELB (elb-abc123.elb.amazonaws.com/page/) instead of foo.com/page/. I know there's something called CName for elb, but I couldn't get it working (http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-domain-names-with-elb.html?). And I'm not sure if this is something with .htaccess.

Here's the route 53 setting of my domain:

foo.com. A ALIAS examplecode.cloudfront.net (examplecode...)
www.foo.com. A ALIAS examplecode.cloudfront.net (examplecode...)

And CouldFront settings:

Alternative Domain Names (CName): foo.com, www.foo.com

Okay, and here's an example of what I'm facing:
Schema: client->cloudfront->elb(beanstalk)->EC2
1. Everything works fine when A client type foo.com or www.foo.com in his/her browser.
2. The Cakephp asks for login when client is attempting to request a secure page foo.com/account/index (e.g. account page), so the redirection is handled by cakephp controller: $this->redirect(array('controller' => 'secure', 'action' => 'login'));.
3. The expecting landing url should be foo.com/secure/login/ bur what turns out is elb-abc123.elb.amazonaws.com/secure/login.

Can someone help me, thanks.

Foi útil?

Solução

Check the return value of env('HTTP_HOST'), that's what CakePHP uses for the base URL by default. It will probably return elb-abc123.elb.amazonaws.com.

If you cannot change that behavior, then, depending on your CakePHP version, use FULL_BASE_URL, App.fullBaseUrl or Router::fullBaseURL() to define the proper base URL on your own.

$s = null;
if (env('HTTPS')) {
    $s = 's';
}
$fullBaseUrl = 'http' . $s . '://foo.com';

In your index.php

define('FULL_BASE_URL', $fullBaseUrl);
Configure::write('App.fullBaseUrl', FULL_BASE_URL);

Or at anytime (assuming the Router class is available):

Router::fullBaseUrl($fullBaseUrl);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top