Waqleh
Waqleh

Reputation: 10157

How to hide subfolder from url addresses on IIS (RewriteRule)?

I am trying to remove /frontend/web and /backend/web from URL on a windows server using IIS and web.config as an alternative to .htaccess

Original .htaccess file that I need to convert to web.config:

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

I was able to remove the /frontend/web only using web.config like so:

<?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>

This created a problem that now all images, css and JS files are no longer loaded, they all give 404

Also this doesn't fix my problem when with /backend/web as this is suppose to Rewrite any url that contains admin to it.

Upvotes: 0

Views: 1601

Answers (1)

Waqleh
Waqleh

Reputation: 10157

The only way I was able to this was by adding all possible rules and in the end the general rule, as the top rules have higher priority

like so:

<?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>

Upvotes: 1

Related Questions