0

I'm trying to send a redirect using the Location header with PHP but my efforts have been in vain. Regardless of what I'm attempting with my code, I decided to try a test in which all that happens is the actual redirect, and it still fails. Here's the code:

<?php
error_reporting(E_ALL);
header("Location: http://www.google.pt", true, '301');

I have seen some similar questions posted here at stack overflow but most of the times it seems to be related with the generation of output before setting the headers, which seems clear that I'm not doing here. Here's what I get in the browser after the response, using safari.

Safari's response

I don't have any errors in my log nor does PHP seem to be generating any warnings whatsoever. What could be wrong?

Best.

Edit: Here's how I'm making the request:

        $.ajax({
            'url': '/api/test.php',
            'type': 'GET',
        });

And here are the details:

enter image description here

Edit: because the duplication flags: I can't return JavaScript, because this is supposed to be consumed by both a website and a mobile app. The mobile app does not run javascript.

André Fratelli
  • 5,920
  • 7
  • 46
  • 87
  • 2
    Are you using JQuery Mobile? If so, this seems to be quite a [common problem](http://stackoverflow.com/a/16351141/4026505). – Jake Lee Sep 22 '14 at 15:46
  • The `test.php` is supposed to be the redirect? Have you inspected the details of the request? What are the response details? – deceze Sep 22 '14 at 15:47
  • What is failing about the `test.php` request? Is that the request which should result in the redirect? Why is it an `XHR` request? – David Sep 22 '14 at 15:47
  • 3
    Are you loading your `test.php` page through javascript / ajax? If so, you cannot redirect in php, you would need to redirect through javascript. – jeroen Sep 22 '14 at 15:47
  • try `header("Location: http://www.google.pt");` (without the extra detail) and see if that works by itself. – KnightHawk Sep 22 '14 at 15:48
  • Have you tried using ob_start(); just before your redirection in case there is something else loading before the redirection kicks in ? –  Sep 22 '14 at 15:52
  • I'm using jQuery, but not mobile. I've also updated my question with other details. – André Fratelli Sep 22 '14 at 15:55
  • Are you preventing the default action of the request for the AJAX call? What is calling the AJAX function? – Jay Blanchard Sep 22 '14 at 15:55
  • @jeroen, you are right! Entering the URI directly in the browser works. But how can I use redirects with ajax then? – André Fratelli Sep 22 '14 at 15:56
  • By returning a data structure that helps the client-side script decide what it should do – f.e. “redirect” by assigning a new value to `window.location`. – CBroe Sep 22 '14 at 15:57
  • Use window.location in your javascript @AndréFratelli - https://developer.mozilla.org/en-US/docs/Web/API/Window.location – Jay Blanchard Sep 22 '14 at 15:58
  • I can't, because I'm also writing an app that will consume this API, so javascript won't be available then. I need the redirect using ajax.. – André Fratelli Sep 22 '14 at 16:07
  • Don't redirect, returning the contents of an external web-site doesn't seem very useful for an API. Have it return consistent information like json that contains the url you want to display. – jeroen Sep 22 '14 at 16:16

0 Answers0