【问题标题】:Migrate ReWrite from .htaccess to web.config将 ReWrite 从 .htaccess 迁移到 web.config
【发布时间】:2016-08-09 19:02:01
【问题描述】:

我需要将一个站点从 linux (apache) 迁移到 windows (iis),但是,我遇到了很大的麻烦:

.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]

当我尝试翻译到IIS平台时,我试过了:

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

我尝试了很多组合,并阅读了 Microsoft 的教程 (http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_conditions),但我无法理解其中的工作原理。现在,网站坏了。有人可以帮我翻译一下吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite iis


    【解决方案1】:

    这里在 web.config 中使用这个我为你将重写转换为 webconfig。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2016-03-08
      • 2013-07-09
      • 2011-03-21
      • 2021-03-26
      • 2011-11-11
      相关资源
      最近更新 更多