0

Aim : I have a small shortener service. I use links generated using this services as part of the content I post on facebook. Facebook's URL scraper tries to generate a preview by locating the end-point URL and pull relevant information.

This, right now works because I simply redirect in PHP with the way it's mentioned below without allowing me to execute JS before that redirection. I want to find out if there is a way to execute that JS.

What can I do? Well! redirect from JS using window.location like this solution here...

What's the problem? If I do that, Facebook's URL scraper can no longer find the final destination url, resulting in a blank preview of my site(specifically the page where JS executes) instead of the destination site. JS execution is a must so can't omit that.

Question : Is there a way I can execute some piece of javascript before I do a redirection via header location and still be able to get correct previews for URLs?

Reference Code - in Laravel Controller

if($row->save()){
    //i was first doing this and doing a redirect from the view.
    //return View::make('pages/redirect')->with('originalUrl',$row->url);

    //but then i realized it wasn't fetching the previews. So i do this now. 
    return Redirect::to($row->url);
    //assuming it internally uses php's header location. I want to use the JS before this redirect.                                
}
Community
  • 1
  • 1
Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68
  • not directly. if you output a location header, then there's no guarantee that the rest of the page will get rendered or any code executed. you'd have to resort to old school methods. `` redirects, javascript timeouts, etc.. – Marc B Feb 12 '15 at 14:57
  • but even with old school methods, facebook wont be able to find the original destination to generate the previews. isn't it? I have been thinking about a lot of stuff but nothing seems to be helping. – Praveen Puglia Feb 12 '15 at 15:02
  • 1
    Have you tried putting the script after the header location? related:[Will all code after redirect header in PHP always get executed?](http://stackoverflow.com/q/7246649/2033671) –  Feb 12 '15 at 15:17
  • well it's not exactly header location.. let me update the question with code. – Praveen Puglia Feb 12 '15 at 15:30
  • I think you should (completely) rephrase your question. Reading the title, I immediatly think: no way; bad idea, won't work. Start with: "I have a page, the facebook scraper will take a preview there ... ". Okay, what has to happen on this page? Possibly all you want is a Ajax submit of a form, or something like that – Emmanuel Delay Feb 12 '15 at 16:21
  • @EmmanuelDelay - I have updated the question with more info. I hope it's clearer now. – Praveen Puglia Feb 12 '15 at 17:11
  • Ah, I see. I can't find any obvious solution, it might require some dirty tricks. How about you detect if a request (to your URL shortner) comes from the facebook crawler; if it does, you redirect with php, otherwise you redirect with JS. acceptable? First we need to see if we can detect (filter) the crawler. – Emmanuel Delay Feb 13 '15 at 15:07

2 Answers2

0

I tried out the idea I mentioned in the comments.

It looks like it works.

<?php
if (strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit") !== false) {
    // this is the facebook crawler
    header('location: http://stackoverflow.com/');
}
else {
    // other clients
    echo '
    <script>
    window.location = "http://stackoverflow.com/"
    </script>
    Hello World
    ';
}
?>
Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17
0

I have finally got around the problem by following what @Austin had mentioned in comments.

I put the script after header location statement in the view and returned that view from controller. That way facebook's scraper found both my script and the content from destination URL as a merged markup( as seen from the facebook url debugger )

I find it kinda funny and weird but it works! :D

Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68