首页 > 解决方案 > 让 PHP 路由库与 Azure 应用服务一起使用?

问题描述

我有一个使用 Steampixel\Route 进行路由的 PHP 应用程序。我无法让它与 azure 一起使用。我创建了另一个 azure 应用服务,看看我是否可以让 Steampixel\Route 或另一个路由库工作。假设我的 index.php 如下:

require_once __DIR__ . '/vendor/autoload.php';

use Steampixel\Route;


Route::add('/testroute', 'onTestRoute');
Route::add('/', 'onIndex');
Route::run('/');

function onIndex() {
    echo 'on index route<br/>';
    echo 'go to <a href="/testroute">testRoute</a> to see if routing works';
}

function onTestRoute() {
    echo 'on test route';
}

在本地运行时,一切正常。但是部署到天蓝色,/testroute 给我一个 404 not found 错误。我在项目根目录中添加了一个 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="redirect all requests" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="index.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

还有一个.htaccess:

DirectoryIndex index.php

# enable apache rewrite engine
RewriteEngine on


RewriteBase /

# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Push every request to index.php
RewriteRule ^(.*)$ index.php [QSA]

依然没有。当部署在 azure 上时,这个基本应用程序仍然无法将我重定向到 /testroute。有谁知道如何让它工作?提前致谢。

标签: phpazure.htaccessazure-web-app-serviceweb-config

解决方案


我也面临同样的问题。请按照步骤解决问题。

部署后转到您的Azure 门户并导航到您的php 应用程序并选择Settings Blade -> Configurations

并单击Path Mapping选项卡并将您的默认物理路径更改为您的应用程序物理路径

例如:

默认物理路径 –site/webroot

您的应用程序物理路径 –site/webroot/{appdirectory}

在此处输入图像描述

保存您的更改。它将重新启动您的应用程序,您可以检查您的应用程序它不能通过找不到路由 404 错误。


推荐阅读