0

I am looking for a client side redirect to another domain in PHP.

The reason I don't want to do a 301 - 302 - 307 etc. redirect is because I want the page to send a 200 HTTP OK response to crawlers.

Usually I use obfuscated JavaScript to do this on a static webhost such as S3 however, I am aware of the fact that crawlers could potentially pick up the JS since it's client side.

Example of the non-obfucated JS code I use;

window.location.href = "http://yourURL.com";

However, it's a lot harder for crawlers to pick up PHP, so I want to do a similar redirect, but in PHP.

If anyone can share his or her opinion on the best way to go about this, that would be great.

Thanks in advance!

Snowlav
  • 325
  • 1
  • 12
  • 26

4 Answers4

1

As stated in PHP manual:

[...] The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

So, teorically, you could do:

<?php
header('HTTP/1.1 201 Created'); // or as of PHP 5.4: http_response_code(201);
header('Location: http://yourURL.com/');

I am not able to test it right now and I'm not sure this would be effective.


Another possibility is to "hard read" the contents of the redirected address and show it on your page:

<?php
$content = file_get_contents("http://yourURL.com/");
echo $content; // this will output the full HTML from the webpage.

Note: the code above will only work if you have the allow_url_fopen = On directive in your php.ini or have permissions to set it programmatically:

<?php
ini_set('allow_url_fopen', 'on');

This will provide a 200 response code.

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
1

PHP can't redirect the page in any other way than by sending a HTTP code, a Javascript or the HTML meta refresh tag. If you're using an obfuscated Javascript, this may be the best way of doing this.

Beat
  • 1,337
  • 15
  • 30
0

you can use the header() function in Php:

<?php

header('Location: http://yourURL.com/');

Docs: http://php.net/manual/en/function.header.php

Dallas62
  • 1
  • 1
0

The header() function is what you're looking for:

<?php
header("Location: http://yourURL.com/");
exit;

Make sure you exit afterwards, because otherwise when the header is ignored by the crawler, your page will still build and send to it, which I assume is not the behavior you're looking for.

Jerbot
  • 1,168
  • 7
  • 18
  • 1
    That's also sending a 302 – Beat Jul 28 '15 at 20:38
  • I was under the assumption that crawlers know how to pick this up. (Try it with http://tools.seobook.com/server-header-checker/ for example, and it'll show where it redirects too) – Snowlav Jul 28 '15 at 20:38
  • 1
    Unless you are pre--detecting the crawler and stopping the script manually from sending a redirect (php or otherwise), the crawler will always be able to detect a redirect. – Jerbot Jul 28 '15 at 20:40
  • If I use obfuscated JS, the crawlers have yet to pick up the redirect. – Snowlav Jul 28 '15 at 20:41
  • 1
    @Snowlav Then I would probably say (without knowing the reasoning behind this) you've already achieved as good a situation as possible. – Jerbot Jul 28 '15 at 20:43
  • For now yes, but I want to stay a step ahead and switch to PHP for as much as possible, since it's almost impossible to index the PHP code for a crawler. – Snowlav Jul 28 '15 at 20:44
  • You are correct in that the crawlers will not be able to index your PHP code, [with some exceptions](http://stackoverflow.com/questions/12142172/apache-shows-php-code-instead-of-executing), as PHP is executed server side, but headers are, as far as I know, neither possible nor desirable to obfuscate. – Jerbot Jul 28 '15 at 20:48
  • It is typically at this point that I recommend either re-evaluating your reason for overriding default behavior, or approaching the problem from another angle. – Jerbot Jul 28 '15 at 20:51