$_SERVER["REQUEST_URI"] 路由仅适用于基础 url 和 returns 404 Not Found on pages

Routing by $_SERVER["REQUEST_URI"] only works for base url and returns 404 Not Found on pages

对于路由,我需要通过 $_SERVER["REQUEST_URI"] 获得 url 输入,这适用于我的基地 url 例如http://xxx.xxx.xxx.xx/api/ 可以 return /api/ 但任何具有更多输入的请求 http://xxx.xxx.xxx.xx/api/testhttp://xxx.xxx.xxx.xx/api/posts/3 return 错误:

Not Found

The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at xxx.xxx.xxx.xx Port 80

我在主根中使用这个 .htaccess /var/www/html/Module rewrite already enabled

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

更新: 我的简单 php 文件为 http://xxx.xxx.xxx.xx/api/index.php

<?php

var_dump($_SERVER['REQUEST_URI']);
die();

// header( "Access-Control-Allow-Origin: *" );

// header( "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" );

// header( "Content-Type: application/json; charset=UTF-8" );

// header( "Access-Control-Allow-Methods: GET, POST, PUT, DELETE" );

// header( "X-Requested-With: XMLHttpRequest" );

// header( "Cache-Control: no-cache, must-revalidate" );

// header( "Pragma: no-cache" );

// header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );

还有/etc/apache2/sites-available/000-default.conf内容

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        Header set Access-Control-Allow-Origin "*"
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html


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

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf





</VirtualHost>

这对我有用,输入多了

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [NC,L,QSA]

Represent your request URI

这是我的完整代码

  <IfModule mod_rewrite.c>

    Options +FollowSymlinks -Indexes
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/ [NC,L,QSA]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ %1 [R=301,L]

  </IfModule>

并且您必须配置您的 apache2.conf

您必须允许全部覆盖而不是要求全部拒绝