我正在 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>
但是当我在规则标记内添加以下代码时,我收到错误消息由于发生内部服务器错误,无法显示页面
。为什么?
<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 Config 中是非法的。重命名一条规则。
另外看看这个节点(这对于调试有好处):
<?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&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 Rewrite 事情就没有意义。
关于php - 当我向 Azure 中的 Web.config 添加重写规则时,为什么会收到 500 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006125/