如何从IIS(RewriteRule)上的URL地址隐藏子文件夹?

时间:2016-06-13 11:52:26

标签: php .htaccess iis web-config

我正在尝试使用 IIS 从Windows服务器上的网址中删除 / frontend / web / backend / web web.config 作为 .htaccess

的替代方案

我需要转换为 web.config 的原始 .htaccess 文件:

Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.*)$ frontend/web/$1 [L]

我只能使用 web.config 删除 / frontend / web ,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="RewriteRequestsToPublic" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/index.php/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

这就产生了一个问题,现在所有图片,css和JS文件都不再加载,它们都给出了 404

使用 / backend / web 时,这并不能解决我的问题,因为这是为了重写包含 admin 的任何网址。

1 个答案:

答案 0 :(得分:1)

我能做到这一点的唯一方法是添加所有可能的规则,最后是一般规则,因为最高规则具有更高的优先级

像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="AdminThemes" stopProcessing="true">
                    <match url="themes/admin(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/{R:0}" />
                </rule>
                <rule name="AdminAssets" stopProcessing="true">
                    <match url="admin/assets/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/assets/{R:1}" />
                </rule>
                <rule name="AdminJs" stopProcessing="true">
                    <match url="admin/js/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/js/{R:1}" />
                </rule>
                <rule name="AdminBootstrap" stopProcessing="true">
                    <match url="admin/bootstrap-tagsinput-latest/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/bootstrap-tagsinput-latest/{R:1}" />
                </rule>
                <rule name="AdminJquery" stopProcessing="true">
                    <match url="admin/jQuery-Tags-Input-master/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/jQuery-Tags-Input-master/{R:1}" />
                </rule>
                <rule name="AdminCss" stopProcessing="true">
                    <match url="admin/css/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/css/{R:1}" />
                </rule>
                <rule name="AdminLadda" stopProcessing="true">
                    <match url="admin/ladda/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/ladda/{R:1}" />
                </rule>
                <rule name="AdminMedia" stopProcessing="true">
                    <match url="admin/media/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/media/{R:1}" />
                </rule>
                <rule name="AdminRewriteRequestsToPublic" stopProcessing="true">
                    <match url="^admin(.+)?$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="backend/web/index.php/{R:0}" />
                </rule>
                <rule name="Themes" stopProcessing="true">
                    <match url="^(.*)?(themes/front)(.*)?$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/{R:0}" />
                </rule>
                <rule name="Assets" stopProcessing="true">
                    <match url="assets/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/assets/{R:1}" />
                </rule>
                <rule name="Ladda" stopProcessing="true">
                    <match url="ladda/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/ladda/{R:1}" />
                </rule>
                <rule name="Foundation" stopProcessing="true">
                    <match url="foundation/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/foundation/{R:1}" />
                </rule>
                <rule name="Css" stopProcessing="true">
                    <match url="css/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/css/{R:1}" />
                </rule>
                <rule name="Media" stopProcessing="true">
                    <match url="media/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/media/{R:1}" />
                </rule>
                <rule name="RewriteRequestsToPublic" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <action type="Rewrite" url="frontend/web/index.php/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
相关问题