【发布时间】:2021-01-19 05:52:45
【问题描述】:
对于路由,我需要通过$_SERVER["REQUEST_URI"] 获取 url 输入,这适用于我的基本 url
例如http://xxx.xxx.xxx.xx/api/ 可以返回 /api/ 但任何输入更多 http://xxx.xxx.xxx.xx/api/test 或 http://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