I am trying to redirect a page using HTTP 410 status using php header function as
header("HTTP/1.1 410 Gone");
header('Location: http://domain.com/down.php');
exit;
The problem is that even if I set a 410 status code...the status code will automatically change to 302
I have also tried with
header('Location: domain.com/down.php', true, 410);
exit;
It shows 410 redirection but the redirected page does not show anything. It shows a blank page.
Does anybody know if there is a solution for this?
The 410 error indicates that the Web server has no forwarding address for the URL, so can provide no redirection to the new Web server. So the blank page you got using the second snippet is correct.
The first code snippet also gives the correct result - Location creates a redirect to a given address.
So there is no errors in your code or in PHP. I'm not sure you do what you realy need =)
header('Location: http://domain.com/down.php', true, 410);
exit;
Should works. You forgot about protocol in URL.
Besides, AFAIU, Yo can use
header('Location: 'http://domain.com/'down.php');
header("HTTP/1.1 410 Gone");
exit;
But Are you sure you need Redirect and 4xx HTTP status?
The last header line is a correct way to set the status code:
header('Location: domain.com/down.php', true, 410);
However, the Location header name requires a fully qualified URL, your URL looks incomplete. That's not valid.
Next to that you don't send any HTTP response body, so the browser can only display a blank page. To display a non-blank page, provide a response body:
header('Location: http://domain.com/down.php', true, 410);
echo '<h1>Gone.</h1>here.';
exit;
Providing a response body is useful, because user-agents do not need to follow Location headers automatically, especially as with the 410 response code RFC 2616, no response header named Location is expected by a HTTP client.
Maybe you're looking for 301 Moved Permanently.
Try to use some proper quotes?
header("HTTP/1.1 410 Gone");
header("Location: http://domain.com/down.php");
exit;
You have to include the error page :
header("HTTP/1.0 410 Gone"); include_once("410.html"); exit();
Related
On my page
/my/conditional/redirect/page/index.php
I have a conditional PHP 302 Redirect:
<?php
if (!in_array($_SERVER['REMOTE_ADDR'], ['369.168.492.123'])) {
// header('HTTP/1.1 403 Forbidden');
header('Location: https://'.$_SERVER['HTTP_HOST'].'/my/redirect/index.php', TRUE, 302);
exit;
}
else {
[... PHP CODE HERE...]
}
?>
I have tried this code:
as above
with and without the TRUE and 302 parameters
with and without exit;
with and without https://'.$_SERVER['HTTP_HOST']
No matter. The redirect always changes the location in the browser URL bar to:
/my/redirect/index.php
while I would greatly prefer it to persist as:
/my/conditional/redirect/page/index.php
What am I missing to ensure the page redirects (via a 302 Redirect) without the browser URL bar location updating?
What am I missing to ensure the page redirects (via a 302 Redirect) without the browser URL bar location updating?
Nothing. That's how HTTP redirects work.
If you want to preserve the URL, then you need to perform an internal redirect or a proxy request so that the server returns the content you want for the URL that was requested.
if I validate html or register web in any serch engine, I get 302 error.
The reason is a header() function. If I take it away, everything is fine with 200 OK status.
So the main problem is that I need this redirection for web to be multilingual.
The logic is next. When user enters the web page for the first time index.php - require_once a file with a function:
function cookies() {
if (!isset($_COOKIE["lang"])){
setcookie('lang','ukr', time()+(60*60*24*31));
header('Location: index.php');
}}
cookies();
so the user sees a page already filed with a deafault language.
If there would be no redirection from require_once file the data from mysql won't be downloaded and user won't see any text.
The question: should I leave this with HTTP 302 or rebuild the whole site/logic not to have any redirects at index page???
302 is not an error. It is the status code for "Found" (aka "The document you asked for is over here"). PHP will insert this for you automatically if you add a Location header (unless insert a status manually, but you don't want a 301 here)
This is the expected response if you are telling people to go and get a different document based on their language preferences.
It is odd to redirect from index.php to index.php though. Presumably you should just return the appropriate document directly instead of redirecting.
I got it. It's actually pretty simple.
The validators don't accept cookies. So they get stuck in a an infinite loop.
You can test this:
delete all your cookies from your computer.
Disable cookies in your browser and try loading your website.
Whenever You use header("location: .... you will get a 302, it's a status and not an error, it's telling the browser that the site has redirected the page:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Read those validators and engines and see if having the 302 is a problem for whatever you are trying to do, normally it shouldn't be.
A dirty way would be to force the header, personally I don't encourage this and don't know what side-effects could it have really, but it could be a quick workaround to trick those engines:
function cookies() {
if (!isset($_COOKIE["lang"])){
setcookie('lang','ukr', time()+(60*60*24*31));
header('Location: index.php');
header('HTTP/1.1 200 OK'); // <--- Forcing the header status
}}
cookies();
I have a php function which adds bad IP's to a MySQL table.
Each page on my site then checks the table and throws a HTTP 401 header if a match is found.
if(badCrawler()){
header("HTTP/1.1 401 Unauthorized");
header("Location: error401.php");
}
Is it possible to do this without changing the url?
Thanks
Sure. Just exit after your 401 header. No need for the header("Location...") at all.
if(badCrawler()){
header("HTTP/1.1 401 Unauthorized");
exit;
}
Side note: 401 typically is used in conjunction with an authentication request.
From the specs:
The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource.
It might be better to use 403 Forbidden to deny access, or even 404 Not Found if you want the bad crawler to think the page doesn't exist any longer:
header("HTTP/1.0 404 Not Found");
exit;
Sending content
Note that your 404 response might result in a blank page in some browsers, see the top answer in this thread for a full explanation of why that is happening. Basically the header is working but it's just up to you to display any HTML content).
The solutions is simple, echo your content (or include a separate file) right before the exit statement.
Aware this is a little old, but it popped up on a google search of "php, 401s".
Is the issue here that when the page redirects to error401.php that page will return a 200 OK, as error401.php loaded fine. If you really want the 401 page to show, how about this?
if(badCrawler()){
header("HTTP/1.1 401 Unauthorized");
include("error401.php");
exit;
}
I'm pretty sure this isn't possible and I need to set headers on login.php if $_GET['reason'] isset but can someone clarify that I can't set response code then change location?
header('HTTP/1.1 403 Forbidden');
header("Location: http://domain.com/login.php?reason=ipbanned");
Right, this is not possible. A "Location" header sets the HTTP response code to 302 Found (or another 3xx redirect code) and you can't send two response codes at a time.
Update:
The correct way to do what you want is to send the forbidden header on the redirected page, i.e. in login.php if $_GET['readon']=='ipbanned', not on the redirecting page.
I havea code that should redirect in case it doesnt have some request parameter set correctly.
if(!is_numeric($_GET['id'])){
header("HTTP/1.0 404 Not Found");
header('Location: '.$url);
exit();
}
Problem is that whenever I check with Firefoxes plugin, live HTTP headers, I see 302 temporary redirect. why is that? why no 404 response is given?
It doesn't make sense to send a Location header with a 404 status code.
Location means "What you asked for is over here"
404 means "I don't have what you asked for"
The two statements are incompatible.
If you want to send a particular human readable explanation of the error, then just output it as you would for any other kind of document. You could include() it if you like. Don't try to redirect to it.
Problem is that whenever I check with Firefoxes plugin, live HTTP
headers, I see 302 temporary redirect. why is that? why no 404
response is given?
Not sure what you want to do here but following will provide your purpose.
if (! is_numeric($_GET['id'])) {
header('Location: ' . $url, true, 404);
exit();
}
You can only send ONE of the headers you are attempting as stated above.
In case of 404 - this will display the error relevant error pages as defined by you're web server config.
To do what you want... you will need to modify the server's 404 from a plain html to a php page and work out what to display from there (you can base on referrer etc).
From within the 'dynamic' 404 page, you can then do you're redirect if required.