views:

21

answers:

1

I have a ZF website at domainA.com and I'd like to alias domainB.com to something like: domainA.com/photos/album so that album would be the root of domainB.

How might this be accomplished?

+1  A: 

The first step here is to rewrite the URL to the correct route using mod_rewrite. Something like the following at the top of your exisitng .htaccess root should work:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}    =domainB.com [NC]
RewriteCond %{REQUEST_URI} !^/photos/album
RewriteRule .* /photos/album/$0 [L]

This will redirect to /photos/album/ if no path is specified after domainB.com. If I remember correctly, Zend Framework isn't picky about whether your controllers/actions are terminated by a slash or not, so this should be fine. If it needs to be /photos/album for some reason, I have a solution for that as well, but it's overkill if it's not needed.

Later, your ruleset translates the URL to index.php, and Zend Framework does its routing. However, the default way Zend Framework does routing is to use REQUEST_URI, which happens to not be what we want in this case (It will be whatever was passed after domainB.com, instead of what we rewrote the request to). We actually need to use REDIRECT_URL, which is set by our new RewriteRule.

The controller request documentation describes this scenario (kind of), and explains that you can use Zend_Controller_Request_Apache404 as a drop-in request object to obtain the correct route information:

$request = new Zend_Controller_Request_Apache404();
$front->setRequest($request);

I'm fairly certain that you could also just get the current request object, and call

$request->setRequestUri($_SERVER['REDIRECT_URL']);

on it. Just condition this operation on the host, and hopefully it should all work out correctly.

Tim Stone
I wonder if an easier solution (certainly one that would be accepted), would be to only allow domainB.com access to /photos/album - all other requests outside of this url would be denied and/or redirected back to /photos/album.
Joshua McGinnis
@Joshua McGinnis - I guess it depends, really. I'd be personally inclined to take this approach for the "cleanness" of the URL, as once it's set up you shouldn't have to mess with it much. Performing a redirection as you've suggested allows you to avoid having to modify the backend portion though, so in that sense it's easier to keep track of what's responsible for making that work.
Tim Stone