views:

689

answers:

2

I've written several Web MVC apps but never before in PHP, and I'm trying to figure out the best way to work with Zend Framework v1.9.5.

I'm confused about the role of .htaccess in a Zend Framework 1.9.5 application. I have consulted many tutorials, books, and SO questions on Zend Framework (but most of them are for v1.8 at newest) and they all talk about the central role of the .htaccess file. I gather that .htaccess can be used to support virtual hosts, URL rewriting, and to allow Apache to serve static files without going through index.php but I'm not sure if this is current practice or still necessary in v1.9.5.

Currently I have written a couple of pretty simple (HTML, CSS, jQuery) Zend Framework apps and created Apache virtual hosts for them in a test Ubuntu Server 9.10 environment. I didn't use any .htaccess files at all and the apps seem to work fine.

Here's what I did so far:

I created my apps using Eclipse/PDT and the zf.sh tool. I added css, images, and js directories to the public directory that 'zf create project' produced. These apps run fine on my local MAMP installation.

On the server, I installed the Zend Framework in /usr/local/Zend/share/ZendFramework-1.9.5 and added /usr/local/Zend/share/ZendFramework-1.9.5/library to 'include_path' in php.ini.

I copied the apps to the server directories /home/myadmin/public_html/domain[12]/com.

I created virtual hosts by adding entries in the Apache available-sites directory as outlined in Slicehost Virtual Host Setup. I assign DirectoryIndex = index.php and DocumentRoot = /home/myadmin/public_html/domain[12]/com/public. Both apps seem to work fine.

Do I need to use .htaccess files? For what?

*** EDIT - My Solution

Based on Richard's link, I moved the rewrite statements that usually live in .htaccess into my virtual host definition, and now my applications don't use a .htaccess file. My domain1.com virtual host file looks like:

<VirtualHost *:80>
  # Define admin email, server name, and any aliases
  ServerAdmin [email protected]
  ServerName domain1.com
  ServerAlias www.domain1.com

  # Only serve files from inside this directory
  DocumentRoot /home/myadmin/public_html/domain1.com/public

  # Directly serve any requested files that exist in DocumentRoot;
  # otherwise redirect the request to the index.php script
  RewriteEngine off
  <Location />
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ /index.php [NC,L]
  </Location>

  # Set up log file locations
  LogLevel warn
  ErrorLog /home/myadmin/public_html/domain1.com/data/logs/error.log
  CustomLog /home/myadmin/public_html/domain1.com/data/logs/access.log combined
</VirtualHost>
+3  A: 

One primary use of .htaccess for zend framework and most php frameworks is to redirect all requests (except to static files) to a bootstrap file. You don't necessarily need it but your URL would end up looking something like /index.php/controller/action as opposed to /controller/action

You could also just add the rewrite rules to you apache config directly.

Chris Gutierrez
ZF 1.9.5 performs its own routing without using .htaccess. By default, if I define an action 'zf create action getit controller', the url is '<vhost>/controller/getit'. It is also possible to do lots of customization with $Router->addRoute() inside index.php.
Keith Morgan
When using the zend framework command line tools it creates the htaccess file in the public directory.
Chris Gutierrez
Right you are! It's strange that Eclipse/PHT doesn't show it but the command line confirmed it. What is the role of this .htaccess file vs. the ZF router?
Keith Morgan
the .htaccess file helps cleanup the url a little bit. There is a single file that bootstraps the application. In most cases, index.php. All requests are sent to this file and the framework (the router) handles them from there sending them to the right controller/action. Without the .htaccess file, you wouldn't be able to use the url domain.com/controller/action. It would have to be domain.com/index.php/controller/action. If you don't want to use a .htaccess file, you can take the rewrite rules and add them to the virtual host entry. Hope this helps!
Chris Gutierrez
+1  A: 

The VirtualHost configuration and .htaccess file are two alternatives for URL rewriting in Zend Framework. They perform the same function and you don't need both.

URL rewriting simply directs all requests for non-static files to your index.php.

Routing is the process of decomposing the request URI and figuring out which module/controller/action should be executed. This is a related but separate function to URL rewriting.

Richard Nguyen
Precise answer with two very helpful links. Thanks.
Keith Morgan