Detect and change external links in PHP?
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 y开发者_StackOverflow中文版ou 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
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
}
You mean like header('Location: http://www.example.com/');
?
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 :)
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" );
精彩评论