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

des*_*shg 5 .htaccess mod-rewrite iis-7 web-config symfony

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

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

不幸的是我很少使用Windows服务器,并且不擅长web.config语法,所以如果有人可以建议从Windows服务器上的所有网址的开头删除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>
Run Code Online (Sandbox Code Playgroud)

转换后的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>
Run Code Online (Sandbox Code Playgroud)

非常感谢!

戴夫

Dou*_*oug 4

在使用 IIS7.5 在 Windows 服务器上设置 symfony2 时,刚刚遇到了同样的问题。诀窍是进入您的网站管理部分。查找“url 重写”,查找“导入规则”(位于右侧某处)。然后在新对话框中默认浏览“SymfonyProject/web/”中的 .htaccess 文件。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>
Run Code Online (Sandbox Code Playgroud)

所以我的 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>
Run Code Online (Sandbox Code Playgroud)

我还设置了我的网络目录作为我的根目录。

希望这可以帮助。道格.