-1

I want to open every external url in a new page by a redirection.

These external links will be shown on my page

www.pppexample.com
www.pp2example.com

I want to open every external url in this format..

http://www.domain.com/redirector.php?url=http://www.anyexternalurl.com...

I currently using PHP to do this but haven't figured it out.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Kapil
  • 115
  • 3
  • 17
  • Do you want to redirect or show the content of the other url into your page? – Rohit Choudhary May 02 '13 at 10:49
  • @RohitKumarChoudhary actually i want to know how many external links are opening in my website that will be done by redirector.php?url= bcoz i will save it to database and one more thing this can save our page rank too.. – Kapil May 02 '13 at 10:56
  • @RohitKumarChoudhary One more thing i want to open external links throw a redirection page.. See i can create redirection page. But i am not able to make a script by which it can detect automatically it is an external url and then just add a redirection code line url in it – Kapil May 02 '13 at 10:58
  • Ok for this you have to redirect every links in your site to your redirect.php where you get the $_GET['url'] parameter value. Now hold for 1 sec in this redirect.php and insert required information into database then redirect. For holding 1 sec you can set redirect time in header. – Rohit Choudhary May 02 '13 at 10:59
  • Where are those external links showing? on your web contents? Or what? Can you specify it clearer? – Willy Pt May 02 '13 at 11:01
  • If you are going to do this, you need to make sure that you only redirect links from your own site. See http://support.google.com/webmasters/bin/answer.py?hl=en&answer=171297 I believe you can view exit pages if you install google analytics, so that may be an easier and safer solution – Anigel May 02 '13 at 11:01
  • @RohitKumarChoudhary I already know that my problem is how to automatically add this line http://www.domain.com/redirector.php?url= before every external link..bcoz i do not want to do that manually.. – Kapil May 02 '13 at 11:02
  • For checking external url make a regex that matches only your website url if regex matches then do not redirect or redirect to your website path otherwise redirect to destination – Rohit Choudhary May 02 '13 at 11:03
  • You have to make script to add that string by making a regular express so that when content loaded into that page it should be appended with your link – Rohit Choudhary May 02 '13 at 11:04

4 Answers4

0

The auto_append_file directive can be set to a file that is executed after your main PHP file. That file can parse the contents of the output buffer, modify links in the way that you want them to be and print out the the modified HTML.

Oswald
  • 31,254
  • 3
  • 43
  • 68
0

You can use preg_replace() function to do this. As there are already question in StackOverflow that is already answered. Please refer to it How to mimic StackOverflow Auto-Link Behavior and this http://css-tricks.com/snippets/php/find-urls-in-text-make-links/

Community
  • 1
  • 1
Willy Pt
  • 1,795
  • 12
  • 20
0

preg_replace is maybe too slow for that. You can use this:

if ('http' === substr($url, 0, 4)) {
    //external
    $url = 'http://example.com/redirect.php?url=' . $url;
}
0

If you are going to do this without editing all your pages then one solution would be to

Use an auto_prepend_file to start output buffering see ob_start()

Using an auto_append_file read the contents of the output buffer and replace the links with the text you want using preg_replace()

Then serve the re placed contents to the end user

Anigel
  • 3,435
  • 1
  • 17
  • 23