What should I use .htaccess for in Zend Framework 1.9.5 application?
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 webmaster@domain1.com
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开发者_Python百科 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>
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.
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.
精彩评论