I am trying to program a URL Shortener in PHP. The domain adds to the db correctly and then shows itself correctly, but I just can’t get the part where they go to the url and it redirects them to work. I keep getting a 500 internal error.
My .htaccess:
XBitHack Off
RewriteEngine On
RewriteCond %{REQUEST_URI} \\/([0-9a-z]{4})$ [NC]
RewriteRule ^(.*) http://localhost/go.php?%1 [L]
My go.php:
<?php
include ("config.php");
$i = $_SERVER['QUERY_STRING'];
if (preg_match("/^[0-9a-z]{6}$/", $i)) {
$result = mysql_query("SELECT short_url, url FROM `urls` WHERE `short_url` = '$i'") or die(mysql_error());
if (mysql_num_rows($result) < 1) {
header("Location: http://localhost/url");
exit;
}else
{
$row = mysql_fetch_row($result);
header("Location: ".$row[1]);
}
}
?>
It is hosted on WAMP and everything seems to be good, rewrite engine is on, I’m so lost, please help!
What do you mean the domain adds to the db correctly and then shows itself? The only SQL you have is to select something from the database. You’ll need to debug and find out which part of the script is causing the 500 error. Have you looked at the error logs?
Everything seems fine in the logs, I just checked and apparently the whole site is getting the internal error. I am guessing it has to be .htaccess file, because when that is removed, it works. However it doesn’t redirect either.