0

I have several basic redirects on apache using mod_rewrite's RewriteRules. I want to do these redirections programatically, using Zend Framework. These redirections may include different domains. E.g.

RewriteRule ^/xmas(.*) http://mycdn.com/things/xmas$1 [P]

When redirecting with Apache, the url stays the same, e.g. on the address bar on the browser. I want to mimic this behaviour in ZF, but cannot find the way for other domains.

I can do it for in-site redirections via extending Zend_Controller_Router_Route, extending the match() method, and then returning an array with module, controller, action, etc. fields.

class My_Route extends Zend_Controller_Router_Route
{
// ...

  public function match($path)
  {
    // ...
    return
      array(
        'module' => 'default',
        'controller' => 'mycontroller',
        'action' => 'index',
    );
  }
}

But I haven't managed to make it work for redirecting to other domains, since it apparently only works for in-site routes (Haven´t found a way to specify domain or full url on the specs either).

I have tried another ways, like:

$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$r->gotoUrl("http://otherdomain.com/some-stuff-to-redirect-to")->redirect();

and this does the redirection, but it totally changes the page making a whole new request.

How could I implement this in ZF? Using ZF1.

xmar
  • 1,729
  • 20
  • 48

1 Answers1

0

The [P] flag on mod_rewrite rules is a new one on me, but from the docs it looks like it tells Apache to proxy the request to the given URL (using mod_proxy). So it actually makes another request to that URL internally and serves it back to the user, it's not a redirect. The equivalent in ZF would be to route the request to an action which uses Zend_Http_Client to make another request to the remote server.

It can't think of any situation in which this would be a good idea. The example you gave is rewriting a request to a remote CDN. By doing this you completely negate all the advantages of having a CDN in the first place (better performance, lower bandwidth costs). Perhaps if you can explain your use case there might be a better way to achieve what you are trying to do.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Re. costs: you mean that the request that is done to the CDN, has to "come back" to the original server before being handed to the user, hence the possible benefits of serving from the CDN to the user are not happening? Re. what I want to do - I'm hosting the home in a cdn statically, but obviously don't want the home URL to appear as the cdn's. Would it be possible to combine this without the performance penalty? – xmar Jan 13 '13 at 12:12
  • I'm assuming by 'home' you mean the homepage. If the user is coming to your server to view the homepage, regardless the proxy stuff you set up, you are not hosting the homepage in a CDN. You may want to look into 'origin pull' CDNs, which support natively what you are trying to setup in code. But if you want the CDN to host your homepage, you have to point your whole domain's DNS at the CDN. It's all or nothing. – Tim Fountain Jan 13 '13 at 15:37
  • Thanks Tim. I'm finally moving the home HTML to my application server too, only leaving the static elements to be served from CDN. Will measure the impact if any. – xmar Jan 14 '13 at 12:08