将web.config转换为htaccess时出错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将web.config转换为htaccess时出错相关的知识,希望对你有一定的参考价值。

我想将此配置文件转换为htacess文件。我正在寻找一种方法将我的重写规则转换为.htaccess文件。我找不到任何自动执行此操作的工具,而我所能得到的只是500内部服务器错误。

web.config文件如下所示:

<configuration>
    <system.webServer>

        <rewrite>
                <rules>
                        <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                                <match url="^index.php$" />
                                <conditions>
                                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                                        <add input="{QUERY_STRING}" pattern="^module=([^=&amp;]+)$" />
                                </conditions>
                                <action type="Rewrite" url="{C:1}" appendQueryString="true" />
                        </rule>
                        <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                                <match url="^([^/]+)/?$" />
                                <conditions>
                                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                                </conditions>
                                <action type="Rewrite" url="index.php?module={R:1}" />
                        </rule>
                        <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
                                <match url="^index.php$" />
                                <conditions>
                                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                                        <add input="{QUERY_STRING}" pattern="^module=([^=&amp;]+)&amp;page=([^=&amp;]+)$" />
                                </conditions>
                                <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
                        </rule>
                        <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
                                <match url="^([^/]+)/page/([^/]+)/?$" />
                                <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                                </conditions>
                                <action type="Rewrite" url="index.php?module={R:1}&amp;page={R:2}" />
                        </rule>
                        <rule name="RedirectUserFriendlyURL3" stopProcessing="true">
                                <match url="^index.php$" />
                                <conditions>
                                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                                        <add input="{QUERY_STRING}" pattern="^module=([^=&amp;]+)&amp;p=([^=&amp;]+)&amp;id=([^=&amp;]+)$" />
                                </conditions>
                                <action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
                        </rule>
                        <rule name="RewriteUserFriendlyURL3" stopProcessing="true">
                                <match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
                                <conditions>
                                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                                </conditions>
                                <action type="Rewrite" url="index.php?module={R:1}&amp;p={R:2}&amp;id={R:3}" />
                        </rule>
                        <rule name="RedirectUserFriendlyURL4" stopProcessing="true">
                                <match url="^index.php$" />
                                <conditions>
                                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                                        <add input="{QUERY_STRING}" pattern="^module=([^=&amp;]+)&amp;p=([^=&amp;]+)&amp;id=([^=&amp;]+)&amp;page=([^=&amp;]+)$" />
                                </conditions>
                                <action type="Redirect" url="{C:1}/{C:2}/{C:3}/{C:4}" appendQueryString="false" />
                        </rule>
                        <rule name="RewriteUserFriendlyURL4" stopProcessing="true">
                                <match url="^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$" />
                                <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                                </conditions>
                                <action type="Rewrite" url="index.php?module={R:1}&amp;p={R:2}&amp;id={R:3}&amp;page={R:4}" />
                        </rule>
                        <rule name="RedirectUserFriendlyURL5" stopProcessing="true">
                                <match url="^index.php$" />
                                <conditions>
                                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                                        <add input="{QUERY_STRING}" pattern="^module=([^=&amp;]+)&amp;p=([^=&amp;]+)&amp;date=([^=&amp;]+)$" />
                                </conditions>
                                <action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
                        </rule>
                </rules>
                <outboundRules>
                        <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIshtml1">
                                <match filterByTags="A, Form, Img" pattern="^(.*/)index.php?module=([^=&amp;]+)$" />
                                <action type="Rewrite" value="{R:1}{R:2}/" />
                        </rule>
                        <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
                                <match filterByTags="A, Form, Img" pattern="^(.*/)index.php?module=([^=&amp;]+)&amp;(?:amp;)?page=([^=&amp;]+)$" />
                                <action type="Rewrite" value="{R:1}{R:2}/page/{R:3}/" />
                        </rule>
                        <rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
                                <match filterByTags="A, Form, Img" pattern="^(.*/)index.php?module=([^=&amp;]+)&amp;(?:amp;)?p=([^=&amp;]+)&amp;(?:amp;)?id=([^=&amp;]+)$" />
                                <action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
                        </rule>
                        <rule name="OutboundRewriteUserFriendlyURL4" preCondition="ResponseIsHtml1">
                                <match filterByTags="A, Form, Img" pattern="^(.*/)index.php?module=([^=&amp;]+)&amp;(?:amp;)?p=([^=&amp;]+)&amp;(?:amp;)?id=([^=&amp;]+)&amp;(?:amp;)?page=([^=&amp;]+)$" />
                                <action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/page/{R:5}/" />
                        </rule>
                        <rule name="OutboundRewriteUserFriendlyURL5" preCondition="ResponseIsHtml1">
                                <match filterByTags="A, Form, Img" pattern="^(.*/)index.php?module=([^=&amp;]+)&amp;(?:amp;)?p=([^=&amp;]+)&amp;(?:amp;)?date=([^=&amp;]+)$" />
                                <action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
                        </rule>
                        <preConditions>
                                <preCondition name="ResponseIsHtml1">
                                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                                </preCondition>
                        </preConditions>
                </outboundRules>
        </rewrite>
        <urlCompression doStaticCompression="true" />
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.24:00:00" />
    </staticContent>
    </system.webServer>
</configuration>

所以我的解决方案是这样的。

RewriteRule ^index.php$    {C:1}
RewriteRule ^([^/]+)/?$    index.php?module=$1
RewriteRule ^index.php$    {C:1}/{C:2}
RewriteRule ^([^/]+)/page/([^/]+)/?$    index.php?module=$1&page=$2
RewriteRule ^index.php$    {C:1}/{C:2}/{C:3}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$    index.php?module=$1&p=$2&id=$3
RewriteRule ^index.php$    {C:1}/{C:2}/{C:3}/{C:4}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$    index.php?module=$1&p=$2&id=$3&page=$4
RewriteRule ^index.php$    {C:1}/{C:2}/{C:3}

总线我在使用这段代码时遇到了一些问题。有些规则确实不起作用。你能帮我解决这个问题吗?

提前致谢!

答案

首先,您应该检查mod_rewrite.c是否在您的服务器中处于活动状态,然后打开它并开始重写这样的URL:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?module=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?module=$1&p=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?module=$1&p=$2&id=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/page/([^/]*)$ /index.php?module=$1&p=$2&id=$3&page=$4 [L]

</IfModule>

以上是关于将web.config转换为htaccess时出错的主要内容,如果未能解决你的问题,请参考以下文章

IIS 7及IIS 7.5下面.htaccess转为web.config的方法

php配置伪静态如何将.htaccess文件转换 nginx伪静态文件

Web Config&EF6&DbFirst&Oracle - 无法将OracleConnection转换为SqlConnection

部署时的 web.config 转换未构建

在windows主机修改web.config文件中将所有html页面处理为php

Web.Config 将单个 AppSettings 转换为 ConfigSource