.htaccess到web.config以进行url重写和重定向

时间:2016-02-17 12:03:56

标签: php .htaccess web-config windows-server-2008

我有域名xyz.com,它托管在Windows服务器上。

xyz.com的代码是用PHP编写的(以前是在ASP.NET中编写的)。数据库是MySQL(以前是在SQL服务器中)。

现在,在用PHP重新开发整个网站后,我发现.htaccess无法在Windows服务器上运行。我必须使用web.config

以下是我在本地用PHP重新开发网站时使用的.htaccess代码:

RewriteRule index.html index.php

RewriteRule news.html news.php
RewriteRule search-results.html search-results.php

RewriteRule ^([A-Za-z0-9_\-]+).html$ pages.php?pageid=$1&%{QUERY_STRING} [ne]

发生了一件奇怪的事情

当我在web.config中添加以下代码行时,它运行正常

    <rules>
                <clear />
                <rule name="Redirect to google.com" stopProcessing="true">
                    <match url="^google$" />
                    <action type="Redirect" url="http://www.google.com/" appendQueryString="false" />
                </rule>
</rules>

上面的代码将我重定向到google.com,这意味着已经启用了重写模块

但是当我将下面提到的代码添加到web.config

 <rules>
            <rule name="REWRITE_TO_PHP">
            <match url="^(.+).html$" />
                <conditions logicalGrouping="MatchAll" />
                <action type="Rewrite" url="pages.php?pageid={R:1}" />
            </rule>

它给了我错误:

HTTP错误500.19 - 内部服务器错误 无法访问请求的页面,因为页面的相关配置数据无效。

任何人都可以帮我创建等效的web.config代码吗?

3 个答案:

答案 0 :(得分:1)

试试这个。

在您的web.config文件中,找到

<rewrite>
     <rules>

这并将代码放在此内。 <rewrite><rules> .. codes here... </rules></rewrite>代码。

<rule name="rule 1y">
    <match url="index.html"  />
    <action type="Rewrite" url="index.php"  />
</rule>
<rule name="rule 2y">
    <match url="news.html"  />
    <action type="Rewrite" url="news.php"  />
</rule>
<rule name="rule 3y">
    <match url="search-results.html"  />
    <action type="Rewrite" url="search-results.php"  />
</rule>
<rule name="rule 4y">
    <match url="^([A-Za-z0-9_\-]+).html$"  />
    <action type="Rewrite" url="pages.php?pageid={R:1}&amp;%{QUERY_STRING}"  />
</rule>

所以文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <configSections>
        <sectionGroup name="system.webServer">
            <sectionGroup name="rewrite">
                <section name="rewriteMaps" overrideModeDefault="Allow" />
                <section name="rules" overrideModeDefault="Allow" />
            </sectionGroup>
        </sectionGroup>
    </configSections>

    <system.webServer>
        <rewrite>
            <rule name="rule 1y">
                <match url="index.html"  />
                <action type="Rewrite" url="index.php"  />
            </rule>
            <rule name="rule 2y">
                <match url="news.html"  />
                <action type="Rewrite" url="news.php"  />
            </rule>
            <rule name="rule 3y">
                <match url="search-results.html"  />
                <action type="Rewrite" url="search-results.php"  />
            </rule>
            <rule name="rule 4y">
                <match url="^([A-Za-z0-9_\-]+).html$"  />
                <action type="Rewrite" url="pages.php?pageid={R:1}&amp;%{QUERY_STRING}"  />
            </rule>
        </rewrite>
    </system.webServer>
</configuration>

注意:请不要直接用web.config文件替换代码。只需将所需的行放在web.config文件中即可。

答案 1 :(得分:0)

礼貌:http://cbsa.com.br/tools/online-convert-htaccess-to-web-config.aspx

<?xml version="1.0" encoding="UTF-8"?>
    <configuration> 
        <system.webServer> 
            <rewrite> 
                <rules>
                    <rule name="rule 1p">
                        <match url="index.html"  />
                        <action type="Rewrite" url="/index.php"  />
                    </rule>
                    <rule name="rule 2p">
                        <match url="news.html"  />
                        <action type="Rewrite" url="/news.php"  />
                    </rule>
                    <rule name="rule 3p">
                        <match url="search-results.html"  />
                        <action type="Rewrite" url="/search-results.php"  />
                    </rule>
                    <rule name="rule 4p">
                        <match url="^([A-Za-z0-9_\-]+).html$"  />
                        <action type="Rewrite" url="/pages.php?pageid={R:1}&amp;%{QUERY_STRING}"  />
                    </rule>
                </rules> 
            </rewrite> 
    </system.webServer> 
</configuration>

答案 2 :(得分:0)

经过R&amp; D很长一段时间并尝试各种不同的方式后我找到了解决方案,这里是我整个web.config的内容

<?xml version="1.0" encoding="UTF-8"?>
<configuration>



    <system.webServer>
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="index.php" />

            </files>
        </defaultDocument>

        <rewrite>
            <rules>

            <rule name="Imported Rule 3" stopProcessing="true">
                <match url="^index\.html$" ignoreCase="false" />
                <action type="Rewrite" url="index.php" appendQueryString="false" />
            </rule> 

            <rule name="Imported Rule 2" stopProcessing="true">
                <match url="^news\.html$" ignoreCase="false" />
                <action type="Rewrite" url="news.php" appendQueryString="true" />
            </rule>

            <rule name="Imported Rule 4" stopProcessing="true">
                <match url="^(.*)\.html$" ignoreCase="false" />
                <action type="Rewrite" url="pages.php?pageid={R:1}" appendQueryString="false" />
            </rule> 





            </rules>
        </rewrite>

    </system.webServer>




    <system.data>
        <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
    </system.data>
</configuration>

对于尝试在Window Server上托管PHP网站的人来说非常有用