1

我正在使用 Zend Framework 2 在 PHP 中开发应用程序。一切都在本地工作(linux上的apache服务器)。

但现在我必须将我的站点部署到 Windows Server(在 Azure 上使用 IIS)。

由于 IIS 不读取 .htaccess 我必须在 web.config 文件上重写它。这就是我使用 IIS7 导入重写规则功能所做的。更多信息在这里

不幸的是,ISS 无法将规则转换为网络配置。

这是我需要你帮助的地方。

这是我的 .htaccess 文件(Zend Framework 2 中的默认文件)

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

编辑

经过一些研究,我发现 -s 和 -l(前两个条件)可以替换为 -f。完美的!

所以我真正的问题集中在最后两条规则上。(ISS 可以转换 E=.. 和 ENV:.. 语法)

4

2 回答 2

1

来自 ZF2 文档:

如果您将 IIS 与 URL 重写模块一起使用,请导入以下内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]

你试过这个吗?

于 2013-07-19T21:54:03.713 回答
0

好的,这是我的最终解决方案。一切正常并符合我的期望(我添加了我不想在 URL 中看到 /public 的事实)。

阅读代码注释以获得更好的理解。

<!-- Rewrite rules -->
<!-- Below is some rules in order to have a beautiful URL on the client side.
    + Each requests to a /public/something urls are transferred to a /something url
    + If the /public/requestFile exists we serve it
    + If the requested file exists  we serve it
    + If no other rules has matched then we send the request to our public/index.php file             (default behavior from Zend Framework 2)
-->
<rewrite>
  <rules>
    <!-- Permanent redirect of requests on /public/something to /something -->
    <rule name="Redirect public " stopProcessing="true">
      <match url="^public/(.*)$" ignoreCase="false" />
      <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
    </rule>

     <!-- If the file/directory requested exist in /public, we serve it. -->
    <rule name="Search in public" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{DOCUMENT_ROOT}/public{URL}" matchType="IsFile" ignoreCase="false" />  
        <add input="{DOCUMENT_ROOT}/public{URL}" matchType="IsDirectory" ignoeCase="false" />
      </conditions>
      <action type="Rewrite" url="/public/{R:1}" />
    </rule>

    <!-- If the file/directory requested exist, we serve it. -->
    <rule name="Direct files" stopProcessing="true">
      <match url="^.*$" />
      <conditions logicalGrouping="MatchAny">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" />
      </conditions>
      <action type="None" />
    </rule>

    <!-- If no previous rules have matched, then we send the request to public/index (url rewriting
        used by Zend -->
    <rule name="Url rewriting" stopProcessing="true">
    <match url="^.*$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="public/index.php" />
    </rule>
  </rules>
</rewrite>

感谢鲁本的帮助!

于 2013-07-22T15:18:25.963 回答