我应该在Zend Framework 1.9.5应用程序中使用什么.htaccess?

时间:2009-11-24 16:11:58

标签: php apache zend-framework

我已经编写了几个Web MVC应用程序但从未在PHP中编写过,我正在尝试找出使用Zend Framework v1.9.5的最佳方法。

我对.htaccess在Zend Framework 1.9.5应用程序中的作用感到困惑。我在Zend Framework上查阅了许多教程,书籍和SO问题(但大多数是最新的v1.8),他们都讨论了.htaccess文件的核心作用。我知道.htaccess可用于支持虚拟主机,URL重写,并允许Apache在不通过index.php的情况下提供静态文件,但我不确定这是当前的做法还是在v1.9.5中仍然是必需的。< / p>

目前,我已经编写了一些非常简单的(HTML,CSS,jQuery)Zend Framework应用程序,并在测试Ubuntu Server 9.10环境中为它们创建了Apache虚拟主机。我根本没有使用任何.htaccess文件,应用程序似乎工作正常。

这是我到目前为止所做的:

我使用Eclipse / PDT和zf.sh工具创建了我的应用程序。我将css,images和js目录添加到生成'zf create project'的公共目录中。这些应用程序在我当地的MAMP安装上运行良好。

在服务器上,我在/usr/local/Zend/share/ZendFramework-1.9.5中安装了Zend Framework,并将/usr/local/Zend/share/ZendFramework-1.9.5/library添加到'include_path'中php.ini中。

我将应用程序复制到服务器目录/ home / myadmin / public_html / domain [12] / com。

我通过在Slicehost Virtual Host Setup中概述的Apache available-sites目录中添加条目来创建虚拟主机。我分配DirectoryIndex = index.php和DocumentRoot = / home / myadmin / public_html / domain [12] / com / public。这两个应用似乎都运行良好。

我需要使用.htaccess文件吗?为了什么?

*** EDIT - My Solution

基于Richard的链接,我将通常位于.htaccess中的重写语句移动到我的虚拟主机定义中,现在我的应用程序不使用.htaccess文件。我的domain1.com虚拟主机文件如下所示:

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

2 个答案:

答案 0 :(得分:3)

.htaccess对zend框架和大多数php框架的一个主要用途是将所有请求(静态文件除外)重定向到引导程序文件。您不一定需要它,但您的URL最终会看起来像/index.php/controller/action而不是/ controller / action

您也可以直接将重写规则添加到apache配置中。

答案 1 :(得分:1)

VirtualHost配置和.htaccess文件是Zend Framework中URL rewriting的两个备选方案。它们执行相同的功能,您不需要两者。

网址重写只会将所有非静态文件请求定向到index.php

Routing是分解请求URI并确定应该执行哪个模块/控制器/操作的过程。这是与URL重写相关但独立的功能。