.htaccess - 如何将 htaccess 规则转换为 IIS7/Symfony 的 web.config?

标签 .htaccess symfony mod-rewrite iis-7 web-config

我有一个在 linux 服务器上开发的 symfony2 项目,并且出于我无法控制的原因将它(不幸的是!)迁移到了 windows 服务器。除了 url 重写之外,这一切都可以正常工作。我尝试使用 IIS URL 重写模块,但在转换大多数规则时它失败了(愚蠢的是我没有保存它失败的列表)。

它大部分工作正常,但 app.php 仍然出现在所有不应该出现的 URL 的开头。所以网址是 domain.com/app.php/correct/path 当它们应该是 domain.com/correct/path

不幸的是,我很少使用 Windows 服务器,也不擅长 web.config 语法,所以如果有人能建议从 Windows 服务器上所有 url 的开头删除 app.php 缺少什么,那将不胜感激!

原始的 .htaccess 文件是:

DirectoryIndex app.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
    RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>

转换后的 web.config 文件当前为:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
                <match url=".?" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
                </conditions>
                <action type="None" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
</configuration>

非常感谢!

戴夫

最佳答案

刚刚在使用 IIS7.5 的 Windows 服务器上设置 symfony2 时遇到了同样的问题。
诀窍是进入您的网站管理部分。寻找'url rewrite',寻找'Import Rules'(在右边的某个地方)。然后在新的对话框中浏览你的 .htaccess 文件,默认在 'SymfonyProject/web/' 中。 IIS 将为尾部斜杠规则抛出错误,因此删除它(没有它似乎一切正常)并按应用。中提琴没有app.php。

请务必将 app.php 添加到您的默认页面列表中。

我使用 Symfony2 附带的标准 htaccess 文件完成了上述操作。

按以下要求进行编辑

在导入对话框中,我有:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /   <-This line needs deleting as IIS states "This directive was not converted because it is not supported by IIS: RewriteBase "
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

所以我的 web.config 的开头看起来像这样:
<defaultDocument enabled="true">
    <files>
        <add value="app.php" />
    </files>
</defaultDocument>
<rewrite>
    <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            </conditions>
            <action type="Rewrite" url="app.php" appendQueryString="true" />
        </rule>
    </rules>
</rewrite>

我也将我的 web 目录设置为我的根目录。

希望这可以帮助。
道格。

关于.htaccess - 如何将 htaccess 规则转换为 IIS7/Symfony 的 web.config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000313/

相关文章:

.htaccess - 在 .htaccess 中强制使用 SSL 和 WWW

php - Laravel 和 htaccess 的重定向 URL 问题

.htaccess - Htaccess 重定向到 https,同时有来自 vue-router 的另一个条件

javascript - tinymce 捆绑返回 Markdown

.htaccess - 如何在 php 中使用 htaccess 更改已工作的 GET 查询并更改请求的 GET 数据

.htaccess - AWS ELB 在没有 index.php 的情况下将 HTTP 重定向到 HTTPS

symfony - 在 Symfony 2 应用程序中使用 Redis 缓存 Doctrine 结果

symfony - FOSUserBundle 中组和角色的干净使用

apache - 被操纵的 URL 是否会导致 RewriteRule 内出现安全问题?

.htaccess - CakePHP 和 VirtualDocumentRoot 问题