Php $服务器[";请求URI";]的路由仅适用于基本url,并返回404页上找不到的内容
对于路由,我需要通过Php $服务器[";请求URI";]的路由仅适用于基本url,并返回404页上找不到的内容,php,apache,.htaccess,routes,uri,Php,Apache,.htaccess,Routes,Uri,对于路由,我需要通过$\u服务器[“REQUEST\u URI”]获取url输入,该服务器适用于我的基本url e、 g.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.4
$\u服务器[“REQUEST\u URI”]
获取url输入,该服务器适用于我的基本url
e、 g.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
我在主根目录中使用了这个.htaccess
,/var/www/html/
并且模块重写已经启用
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
更新:
我的简单php
文件为http://xxx.xxx.xxx.xx/api/index.php
这对我来说很有效,输入的信息不止这些
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>
选项+FollowSymlinks-索引
#打开mod_rewrite
重新启动发动机
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[NC,L,QSA]
RewriteCond%{THE_REQUEST}^[A-Z]{3,}\s(.*)/index\.php[NC]
重写规则^1[R=301,L]
您必须配置apache2.conf
你必须允许覆盖全部而不是要求全部拒绝我认为你的php代码有问题,你能提供吗?我不认为,我刚刚更新了这篇文章并添加了我的简单php代码,正如您所看到的,我在文件的开头使用var_dump,只返回/api/
作为根目录,对于其他输入不起作用。您是否在apache2.conf中启用了模块重写?您是否允许在您的目录中访问.htaccess?感谢@Jerson的跟进,是的,我做到了,正如我在postModule rewrite中提到的,已经启用了
,我认为问题在于服务器配置,因为对于基本url输入没有任何问题,并且响应数据正确。我还尝试了您更新的.htaccess
并得到500
错误,您也可以使用我的代码,谢谢您可以标记为答案,我将编辑答案,以供将来参考谢谢
<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>