Jump to content

URL shortening service


alex3

Recommended Posts

I have URL shortening service built that I've combined with a file upload service to give users short URLS (domain.com/xyz12) for their uploaded files. This is fine for shortening URLS, but for the locally hosted files (i.e. the uploaded files) the upload script redirects to the file, showing the real file location (domain.com/files/username/file.ext) which is very undesirable, as you can imagine.

 

I've got the rewrite engine redirecting the short URLs to a redirect.php file, which uses the alias (the short part, xyz12) to retrieve the real URL from a MySQL database and redirect to this real URL.

 

redirect.php:

<?php
// Get the DB connection details and some functions
require_once("includes/config.php");
require_once("includes/functions.php");

// Custom connect to db function
db_connect();

$alias = trim(mysql_real_escape_string($_GET['alias']));

if (!preg_match("/^[a-zA-Z0-9_-]+$/", $alias)) {
  header("Location: ".SITE_URL, true, 301);
  exit();
}

// The function get_url is what retrieves the actual URL based on the alias
if (($url = get_url($alias))) {
    header("Location: $url", true, 301);
    exit();
}

header("Location: ".SITE_URL, true, 301);

 

My problem is that I don't know how to hide the location of local files. I can think of a way to start this (I mean, I can easily detect whether the proper URL is a local one) but I can't think of a way to keep this hidden, only showing the alias.

 

This link shows the desired effect, no iframes or anything.

Link to comment
Share on other sites

Use something such as this instead to ensure the user is prompted with a download on a file..

 

<?php
header('Content-type: application/zip');

// It will be called downloaded.zip.. if needed.
header('Content-Disposition: attachment; filename="downloaded.zip"');

// Source of original file..
readfile('./files/user/original324_3423.zip');
?>

Link to comment
Share on other sites

I would assume though that page the user goes to, if viewing a picture say, would be redirect.php. I was thinking you could create a new file on the fly called what ever the alias is, but that seems a very inefficient way of doing it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.