我正在尝试编写一个简单的 RESTful php 应用程序。我正在尝试编写一个 index.php 路由器。
我目前拥有的.htaccess文件是
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule api/^(.*)$ api/index.php?_url=/$1 [QSA,L]
</IfModule>
我添加了 RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
以强制使用尾部斜线 here .
调用 var_dump($_POST);
或 var_dump($_GET);
仍然返回一个空数组,POST 值仍然被丢弃。在评估时发送 GET 和 POST 都会返回 GET:$_SERVER['REQUEST_METHOD'];
。
如何正确实现?另外,我不能使用库,必须实现我自己的路由器。
感谢任何帮助或建议。
EDIT:
从头开始:
请求的 URL 应该是 http://localhost/api/*
位于 api 文件夹中的当前 .htaccess 文件如下所示:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ controller.php?do=$1 [L,QSA]
controller.php
位于 /var/www/api
/etc/apache2/sites-available 中的 Vhost 文件
标题为api.conf
看起来像这样:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /var/www/api/
<Directory "/var/www/api/">
Allow from all
AllowOverride all
Options +Indexes
</Directory>
</VirtualHost>
还是遇到同样的问题:
The requested URL /api/something was not found on this server.
最佳答案
我解决了这个问题:
当我执行我忽略的更新时,Apache 覆盖了 sites-available 文件夹中的 default.conf
文件...
我需要添加到 000-default.conf
<Directory /var/www/>
AllowOverride All
我发现 mod_rewrite 现在关闭了:
in_array('mod_rewrite', apache_get_modules());
//or
var_dump(apache_get_modules());
所以要重新启用:
a2enmod rewrite
service apache2 restart
希望这对其他人有帮助。
关于PHP Rest API,重定向 POST 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29377629/