2

I have a free PHP webserver and I would like to provide a redirect to external links page, just like deviantart.com does. Is there any way to do this with just PHP? I have no access to the server.

Edit: I meant a page asking "Are you sure you want to leave [MA WEBSITE]? NOPE ; DUH - GO TO http://outside-example.com"

Edit2: I actually meant a function to catch outside links and replace them with a /redirect/?url=PARSED_URL_ADDRESS

Vercas
  • 8,931
  • 15
  • 66
  • 106
  • I don't understand your question. Only thing that comes into mind now is header('Location:http://www.website.com/'); But I guess you need something else. – afaf12 Jul 05 '11 at 12:22
  • He has a forum and when the forum fetches the post text, he wants to replace all external links with a redirector. – sinni800 Jul 05 '11 at 12:41

4 Answers4

1

You mean like header('Location: http://www.example.com/');?

tdammers
  • 20,353
  • 1
  • 39
  • 56
1

You need to detect if there is any link which redirects to outside website then you need a page to show something like "Now Leaving yourwebsite.com"

If that is the case then you need to analyze the content of your page before rendering and find out if there is any tags and replace ref of them with some gatway.php?url=outgoing-url

Where in gateway.php compare if the url belongs to your website or external website by using string comparison methods

Use this js code in footer (I am expecting there is some common footer page)

var urls = document.getElementsByTagName("a");
for (urlIndex in urls ) {
   urls[urlIndex].href = "dummy.php?url="+urls[urlIndex].href; //replace dummy.php with urs
}
Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55
0

Provide, for example, a function that creates <a> tags. Or just one that converts URLs to your redirector: redirect.php?url=http://.... The redirector then issues a HTTP header called "refresh" set to the new address.. Beautify it so the user knows he is being redirected, voilá.

Find out yourself how :)

sinni800
  • 1,451
  • 14
  • 29
  • I'd like the converter! But how do I do it? xD – Vercas Jul 05 '11 at 12:25
  • It's not hard. Just make a php that contains your functions, include it in all other PHP files, and put a function like this there: function convertExternalUrl(url) { echo "/redirector.php?url=" . url } . It's pretty simple, so you could also just put the url there. Sorry; I accidentially hit return too early. – sinni800 Jul 05 '11 at 12:26
  • I need a function to automatically parse all the URLs. I cannot write PHP code in forums man, and, even if I could, I would be an idiot to let my users do that. – Vercas Jul 05 '11 at 12:28
  • Alright, sorry. I thought you have the power over your page. In this case, you will have to get into the forum code where the post text is being fetched. There, you can use a regex function such as preg_replace. There's a site where you can get regexes from, which you can match: http://regexlib.com/Search.aspx?k=URL . I'm not that savy in regex myself, but this is basically what you will have to do. – sinni800 Jul 05 '11 at 12:36
0

The best way to do it is using the location header, but you also need to set a 301 response code, this also tells the search engines crawling the link that the content at that url is at a different location, and it's a best practice to set the response code for redirects in general.

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" ); 
bigblind
  • 12,539
  • 14
  • 68
  • 123