I'm creating an API, and I'm testing CRUD the functionality with an external client.
To be able to skip index.php in the URL, and only use '.../producer/id' etc I changed redirection in the .htaccess file (se below) which works accept for the fact that no other header code than 201 - Created can be sent out from the server. If I try to send out any other code it automatically becomes the following code:
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
As an example is the php code below. If I add a '!' before $result so that the statement becomes false the header in the else statement should be sent out, which doesn't happen. Again, this 302 header is sent out. If I leave it as is the 201 header is sent out, just as it should.
Why is this happening?
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
}
.htaccess:
<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>
Location: always triggers a redirect, which is always a 3xx status code.
If you want to output any other code, append it to the other header, and don't send a Location:
if ($result) {
header('HTTP/1.1 201 Created', 201);
} elseif ($errors) {
header('Location: /theform.php'); // 301 by default
} else {
header('HTTP/1.1 500 Internal Server Error', 500);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.