【问题标题】:Routing by $_SERVER["REQUEST_URI"] only works for base url and returns 404 Not Found on pages$_SERVER["REQUEST_URI"] 路由仅适用于基本 url 并返回 404 Not Found on pages
【发布时间】:2021-01-19 05:52:45
【问题描述】:

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

Not Found

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

我在主根/var/www/html/ 也使用这个.htaccess 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>

【问题讨论】:

    标签: php apache .htaccess routes uri


    【解决方案1】:

    这对我有用,不仅仅是输入

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

    $1 代表你的请求 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/$1 [NC,L,QSA]
    
        RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
        RewriteRule ^ %1 [R=301,L]
    
      </IfModule>
    

    你必须配置你的 apache2.conf

    您必须 Allow Override All 而不是 Require all denied

    【讨论】:

    • 我认为你的php代码有问题,你能提供吗?
    • 我不认为,我刚刚更新了这篇文章并添加了我的简单 php 代码,你可以看到我在文件开头使用 var_dump 并且只返回 /api/ 作为根目录并且不适用于其他输入
    • 您是否在 apache2.conf 中启用了模块重写?你也允许在你的目录中使用 .htaccess 吗?
    • 感谢@Jerson 跟进,是的,我做到了,正如我在帖子Module rewrite already enabled 中提到的那样,我认为问题在于服务器配置,因为对于基本 url 输入没有任何问题并且响应正确的数据。我还尝试了您更新的.htaccess 并得到了500 错误
    • 您也可以使用我的代码,谢谢您可以标记为答案,我将编辑答案,以供将来参考谢谢
    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    相关资源
    最近更新 更多