.htaccess 如何将htaccess规则转换为用于IIS 7/Symfony的web.config?

8iwquhpp  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(146)

我有一个在linux服务器上开发的symfony 2项目,由于我无法控制的原因,我把它迁移到了windows服务器上。除了url重写之外,这一切都正常工作。我试着使用IIS URL重写模块,但是在转换大多数规则时失败了(愚蠢的是,我没有保存失败的规则列表)。
大多数情况下运行正常,但app.php仍然出现在所有URL的开头,这是不应该的domain.com/app.php/correct/pathdomain.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>

非常感谢!
戴夫

hec6srdp

hec6srdp1#

我刚刚在使用IIS7.5在Windows服务器上设置symfony 2时遇到了同样的问题。诀窍是进入网站管理部分。查找“url重写”,查找“导入规则”(在右边的某个地方)。然后在新的对话框中,浏览默认情况下位于“SymfonyProject/web/”中的.htaccess文件。IIS将抛出一个尾部斜杠规则的错误,因此请删除(没有它,一切似乎都工作得很好),然后按Apply. Viola no app.php.
请务必将app.php也添加到默认页面列表中。
我使用Symfony 2附带的标准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目录作为我的根目录。
希望这能帮上忙道格。

tkqqtvp1

tkqqtvp12#

我知道这是一个老问题,但今天我有同样的问题,我到处搜索,并花了我很多时间来修复它,通过替换Web目录中的.htaccess文件到Web.config文件与此内容,希望这将有助于一些人。

<?xml version="1.0"?>
    <configuration>
        <system.webServer>
            <defaultDocument>
                <files>
                    <clear />
                    <add value="app.php" />
                </files>
            </defaultDocument>
            <rewrite>
                <rules>
                    <rule name="Symfony Front Controller" 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>
        </system.webServer>
    </configuration>

相关问题