为什么在Azure中向Web.config添加重写规则时会得到500


Why do I get a 500 when I add a rewrite rule to my Web.config in Azure?

我正在Azure上部署站点,并试图为该站点编写重写规则。

我有以下代码,它可以工作:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Sendy all" stopProcessing="true">
          <match url="^(.+?)$" ignoreCase="true" />
             <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             </conditions>
          <action type="Rewrite" url="index.php" appendQueryString="true" />
        </rule>
        <rule name="Sendy all" stopProcessing="true">
          <match url="^api/(.+?)$" ignoreCase="true" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
          <action type="Rewrite" url="index.php" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

但是当我在rules标签内添加以下代码时,我得到错误消息CCD_。为什么?

<rule name="Sendy all" stopProcessing="true">
  <match url="^api/(.+?)$" ignoreCase="true" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
  <action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>

我认为第一版不起作用。第一版的第二条规则与第二版相同。

您的问题是,规则具有相同的名称属性Sendy all。这在Web配置中是非法的。重命名一条规则。

还可以看看这个节点(这有利于调试):

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Detailed" />
    </system.webServer>
</configuration>

请确保,这是web.config中的第一件事。然后,您将获得错误代码500.52,其中包含详细的描述,指示此错误。

顺便说一句,我不明白你在做什么。考虑将请求提供给您的索引PHP:

    <rule name="Sendy all" stopProcessing="true">
      <match url="^api/(.+?)$" ignoreCase="true" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
      <action type="Rewrite" url="index.php?api=1&amp;request={R:1}" appendQueryString="true" />
    </rule>

如果你打电话给website.tld/api/IBar/DoFoo,你可以写:

$isApi = isset($_GET['api']); //Was the API Rule Affected?
$request = isset($_GET['request']) ? $_GET['request'] : ''; //"IBar/DoFoo"

如果你不提取细节,并将其作为参数,那么整个Url重写的事情就没有意义了。