我正在创建一个 API,并且正在使用外部客户端测试 CRUD 功能。
为了能够跳过URL 中的index.php,并且只使用 '.../producer/id' 等,我更改了 .htaccess 文件(见下文)中的重定向,该文件可以接受除了 201 之外没有其他标头代码的事实- 创建的可以从服务器发送出去。如果我尝试发送任何其他代码,它会自动变为以下代码:
HTTP/1.1 302 Found Date: Tue, 04 Dec 2012 19:42:10 GMT Server: Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 X-Powered-By: PHP/5.3.1 Location: /producers/ Content-Length: 565 Content-Type: application/json
例如下面的 php 代码。如果我添加一个“!” 在 $result 之前,这样语句就会变为 false,else 语句中的标头应该被发送出去,这不会发生。同样,这个 302 标头被发送出去。如果我保持原样发送 201 标头,就像它应该发送的那样。
为什么会这样?
php:
if ($result) {
header('HTTP/1.1 201 Created');
header('Location: /producers/' . $id);
}
else {
header('HTTP/1.1 500 Internal Server Error');
header('Location: /producers/' . $id
}
.ht 访问:
<IfModule mod_rewrite.c>
RewriteEngine On
# Allow images and statics to be addressable
RewriteCond $1 !\.(gif|jpe?g|png|css|js)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# You have to manually change to our username or directory where our controller index is host
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>