简体   繁体   中英

Migrate ReWrite from .htaccess to web.config

I need to migrate a site from linux (apache) to windows (iis), BUT, I had a big trouble with it:

.htaccess

RewriteEngine On
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule ^$ index.php [NC,L]
RewriteRule ^/$ index.php [NC,L]
RewriteRule ^download/(.+)$ download/$1 [NC,L]
RewriteRule ^([0-9]+)?$ index.php?c=resetPassword&m=show&id=$1 [NC,L]
RewriteRule ^([^/.]+)?$ index.php?c=$1 [NC,L]
RewriteRule ^([^/.]+)/?$ index.php?c=$1 [NC,L]
RewriteRule ^([^/.]+)/([A-z]+)?$ index.php?c=$1&m=$2 [NC,L]
RewriteRule ^([^/.]+)/([A-z]+)/?$ index.php?c=$1&m=$2 [NC,L]
RewriteRule ^([^/.]+)/([0-9]+)?$ index.php?c=$1&m=show&id=$2 [NC,L]
RewriteRule ^([^/.]+)/([0-9]+)/?$ index.php?c=$1&m=show&id=$2 [NC,L]
RewriteRule ^([^/.]+)/([A-z]+)/([0-9]+)?$ index.php?c=$1&m=$2&id=$3 [NC,L]
RewriteRule ^([^/.]+)/([A-z]+)/([0-9]+)/?$ index.php?c=$1&m=$2&id=$3 [NC,L]

When I tried to translate to IIS platform, I tried it:

    <rewrite>
        <rules>
            <rule name="block favicon" stopProcessing="true">
                <match url="favicon\.ico" />
                <action type="CustomResponse" statusCode="404" subStatusCode="1" 
                    statusReason="The requested file favicon.ico was not found" 
                    statusDescription="The requested file favicon.ico was not found" />
            </rule>
            <rule name="Rule Mapping Root" stopProcessing="true">
                <match url="^/" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="^/" />
                </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>
            <rule name="Rule Mapping Download" stopProcessing="true">
                <match url="^download/(.+)$" ignoreCase="true" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="^download/(.+)$" />
                </conditions>
                <action type="Rewrite" url="download/{R:1}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Reset Pass" stopProcessing="true">
                <match url="^([0-9]+)$" ignoreCase="true" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="^([0-9]+)" />
                </conditions>
                <action type="Rewrite" url="index.php?c=resetPassword&m=show&id={R:1}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 10" stopProcessing="true">
                <match url="^([^/.]+)$" ignoreCase="true" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="^([^/.]+)$" />
                </conditions>
                <action type="Rewrite" url="index.php?c={R:1}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 11" stopProcessing="true">
                <match url="^([^/.]+)/$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 20" stopProcessing="true">
                <match url="^([^/.]+)/([A-z]+)$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m={R:2}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 21" stopProcessing="true">
                <match url="^([^/.]+)/([A-z]+)/$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m={R:2}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 30" stopProcessing="true">
                <match url="^([^/.]+)/([0-9]+)$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m=show&id={R:2}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 31" stopProcessing="true">
                <match url="^([^/.]+)/([0-9]+)/$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m=show&id={R:2}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 40" stopProcessing="true">
                <match url="^([^/.]+)/([A-z]+)/([0-9]+)$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m={R:2}&id={R:3}" appendQueryString="true" />
            </rule>
            <rule name="Rule Mapping Index 41" stopProcessing="true">
                <match url="^([^/.]+)/([A-z]+)/([0-9]+)/$" ignoreCase="true" />
                <action type="Rewrite" url="index.php?c={R:1}&m={R:2}&id={R:3}" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>

I tried so many combinations and I read the Microsoft's tutorial ( http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_conditions ), but I couldn't understand the working for that. Now, the site is broke. Could someone help me with this translation?

Here use this in web.config I converted the rewrite to webconfig for you.

<rule name="rule 1f">
    <match url="%{REQUEST_FILENAME}"  />
    <action type="Rewrite" url="/!-f"  />
</rule>
<rule name="rule 2f">
    <match url="%{REQUEST_FILENAME}"  />
    <action type="Rewrite" url="/!-d"  />
</rule>
<rule name="rule 3f" stopProcessing="true">
    <match url="^$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php"  />
</rule>
<rule name="rule 4f" stopProcessing="true">
    <match url="^/$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php"  />
</rule>
<rule name="rule 5f" stopProcessing="true">
    <match url="^download/(.+)$"  ignoreCase="true" />
    <action type="Rewrite" url="/download/{R:1}"  />
</rule>
<rule name="rule 6f" stopProcessing="true">
    <match url="^([0-9]+)?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c=resetPassword&amp;m=show&amp;id={R:1}"  />
</rule>
<rule name="rule 7f" stopProcessing="true">
    <match url="^([^/.]+)?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}"  />
</rule>
<rule name="rule 8f" stopProcessing="true">
    <match url="^([^/.]+)/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}"  />
</rule>
<rule name="rule 9f" stopProcessing="true">
    <match url="^([^/.]+)/([A-z]+)?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m={R:2}"  />
</rule>
<rule name="rule 10f" stopProcessing="true">
    <match url="^([^/.]+)/([A-z]+)/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m={R:2}"  />
</rule>
<rule name="rule 11f" stopProcessing="true">
    <match url="^([^/.]+)/([0-9]+)?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m=show&amp;id={R:2}"  />
</rule>
<rule name="rule 12f" stopProcessing="true">
    <match url="^([^/.]+)/([0-9]+)/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m=show&amp;id={R:2}"  />
</rule>
<rule name="rule 13f" stopProcessing="true">
    <match url="^([^/.]+)/([A-z]+)/([0-9]+)?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m={R:2}&amp;id={R:3}"  />
</rule>
<rule name="rule 14f" stopProcessing="true">
    <match url="^([^/.]+)/([A-z]+)/([0-9]+)/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php?c={R:1}&amp;m={R:2}&amp;id={R:3}"  />
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM