将web.config转换为htaccess时出错

时间:2017-12-16 14:44:02

标签: .htaccess mod-rewrite

我想将此配置文件转换为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}

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

<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>
相关问题