0

I am having some issues with PHP detecting http:// containing and non-http:// containing links from database and referencing user based on them here is the code;

$url = $_GET['short'];

$route_url = mysql_result(mysql_query('SELECT original_url FROM ' . DB_TABLE . ' WHERE short_url = "' . mysql_real_escape_string($url) . '"'));

mysql_query('UPDATE ' . DB_TABLE . ' SET refferals = refferals + 1 WHERE short_url = "' . mysql_real_escape_string($url) . '"');

if(preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $route_url)) { 
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $route_url);
} else {
$protocol = "http://";
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $protocol.$route_url);
}
mysql_close();
exit;

Can some on tell me what I am doing wrong and how to fix it, so both http:// and non-http:// (google.com) work with redirect

  • What errors are you getting? What do you mean non-http? – Zevi Sternlicht Feb 05 '13 at 21:25
  • This may help http://stackoverflow.com/questions/14698781/how-can-i-change-the-scheme-of-a-url-with-preg-match – Rob Feb 05 '13 at 21:27
  • @InGodITrust non-http means if the link in db is just google.com it will be routed via location. In my case non http:// containing links are routed to mydomain.com/google.com which is the problem. if the link does have http://google.com in db it will work but if not it will fail – 10010010001011101111 Feb 05 '13 at 21:28
  • @proxy do you mean you want it to go to Google and it is going local on your site? – Zevi Sternlicht Feb 05 '13 at 21:29
  • @InGodITrust I am doing a link shortner if that will simply the entire thing all together. But my routing does not route shortened links. It keeps failing when user has a link in database without http:// and based on feedback people did not always want to add http://link while shortning – 10010010001011101111 Feb 05 '13 at 21:31
  • Still not clear what you want and what the problem is – Zevi Sternlicht Feb 05 '13 at 21:32
  • @InGodITrust if I submit a link to the shorting service lets say youtube.com the link is added to the SQL along with short URL so when I copy the link generated by shoring service you are doing: site.ltd/SHORT URL. In theory the site.ltd would then do a 301 and send you to youtube.com. In my case the 301 sends me to site.ltd/YOUTUBE.COM do you get it now? If I submit a link without http its added to database as link.com and this links do not work because my routing can 301 links without http:// in-front of them – 10010010001011101111 Feb 05 '13 at 21:35
  • So why not add your own http in front of the link? – Zevi Sternlicht Feb 05 '13 at 21:38
  • 1
    @proxy A solution would be to force or strip the "http://" part from the URL *before* inserting it in the database, that way you have consistent URLs. – Supericy Feb 05 '13 at 21:40
  • @InGodITrust look at the script its very self explanatory. And I provided more then enough details. Why did I do the else function? Its the first part in if(preg_match that is not working to check if the link already has http if not it goes to else – 10010010001011101111 Feb 05 '13 at 21:40
  • @Supericy thank you problem fixed, I will just do it that way – 10010010001011101111 Feb 05 '13 at 21:43

1 Answers1

0

As suggested by @Supericy to strip http:// from initial URL submission I added following

function http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}

And now the routing works just fine