【问题标题】:.htaccess to web.config for url rewrite and redirection.htaccess 对 web.config 进行 url 重写和重定向
【发布时间】:2016-02-17 12:03:55
【问题描述】:

我的域 xyz.com 托管在 Windows 服务器上。

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

现在用 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 代码?

【问题讨论】:

  • 谁能帮忙?从 8 小时开始我就陷入了这些困境!

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


【解决方案1】:

试试这个。

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

<rewrite>
     <rules>

这个并把代码放在这个里面。 &lt;rewrite&gt;&lt;rules&gt; .. codes here... &lt;/rules&gt;&lt;/rewrite&gt; 标签。

<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 文件中即可。

【讨论】:

  • 供您参考,我的整个 php 代码已上传到 xyz.com/demo/ 您上面提到的代码没有给我任何错误,但是当我在 url 中键入 xyz.com/demo/index.html 时正在将我重定向到未找到的页面
  • 请从&lt;action type="Rewrite" url="/demo/index.php" /&gt;&lt;action type="Rewrite" url="index.php" /&gt; 之类的操作中更改网址...
  • 我根据您的要求编辑代码。我从 &lt;action type="Rewrite" url="/index.php" /&gt; 中删除了 /index.php 之前的斜杠 .. 所以代码现在看起来像 &lt;action type="Rewrite" url="index.php" /&gt;
  • @user3782114 我的代码有效吗?或不?如果工作那么请接受我的回答。因为它也会帮助其他用户。
【解决方案2】:

礼貌: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>

【讨论】:

  • 我试过这个去跟随错误“HTTP错误500.19 - 内部服务器错误请求的页面无法访问,因为页面的相关配置数据无效。”
  • 你是否正确封装了xml?
【解决方案3】:

经过长时间的研发并尝试了各种不同的方法我找到了解决方案,这是我整个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 网站的人来说非常有用

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2015-07-28
    • 2016-09-28
    • 2013-01-27
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2013-12-21
    相关资源
    最近更新 更多