我有一个在Linux服务器上开发的symfony2项目,由于无法控制的原因将其迁移(不幸的是!)到Windows服务器。除url重写外,所有这些均应按其应有的方式工作。我尝试使用IIS URL重写模块,但是在转换大多数规则时失败了(愚蠢的是,我没有保存失败的规则列表)。

它通常可以正常工作,但是app.php仍会出现在所有不应该出现的所有URL的开头。因此,当网址应为domain.com/correct/path时,其网址为domain.com/app.php/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重写”,查找“导入规则”(在右侧某处)。然后,在新对话框中,默认情况下在“SymfonyProject/web/”中浏览您的.htaccess文件。 IIS的斜杠规则将引发错误,因此删除该斜杠(如果没有斜杠规则,一切似乎都可以正常工作),然后按Apply。中提琴没有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目录设置为我的根目录。

希望这可以帮助。
道格

10-08 06:33
查看更多