Php 将Altorouter与.htpasswd基本身份验证一起使用

Php 将Altorouter与.htpasswd基本身份验证一起使用,php,api,rest,altorouter,Php,Api,Rest,Altorouter,My index.php require_once 'altorouter.php'; $router = new Router(); $router->map('GET', '/', function() { require 'home.php'; }, 'home'); $router->map('GET|POST', '/dashboard/[*:serial]?', function($serial) { require 'dashboard.php';

My index.php

require_once 'altorouter.php';
$router = new Router();

$router->map('GET', '/', function() {
    require 'home.php';
}, 'home');

$router->map('GET|POST', '/dashboard/[*:serial]?', function($serial) {
    require 'dashboard.php';
}, 'dashboard');

$match = $router->match();

if (is_array($match)) {

    header("HTTP/1.1 200 OK");
    call_user_func_array( $match['target'], $match['params'] );
        
}

else {

    header('Content-type: text/javascript');
    header("HTTP/1.1 400 Bad Request");
    echo json_encode(array('message' => 'Please check validity of methods and URL slugs.', 'debug' => ($_SERVER['REQUEST_URI'])), JSON_UNESCAPED_UNICODE, JSON_UNESCAPED_SLASHES);

}
Apache站点可用ssl conf文件

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html/>
            #Options Indexes FollowSymLinks
            #AllowOverride All
            #Require all granted
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

        ServerName example.com
        Include /etc/letsencrypt/options-ssl-apache.conf
        ServerAlias www.example.com
        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    </VirtualHost>
</IfModule>
启用基本身份验证时,home.php可访问(登录后),但dashboard.php不可访问

        <Directory /var/www/html/>
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Directory>
禁用基本身份验证后,一切正常

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

选项索引跟随符号链接
允许超越所有
要求所有授权
https://www.example.com/dashboard/abcd123/
返回200和页面


有没有办法将Altorouter与Basic Auth结合起来?为什么“/”路由不受身份验证的影响,而其他路由受身份验证的影响?

显然,
AllowOverride All
需要启用。现在路由工作了

Not Found

The requested URL was not found on this server.
        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>