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.
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 usesZend_Http_Clientto 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.